1

我正在使用Firebase通知来通知用户有关发布到应用程序的新数据。我遵循这里给出的例子:https://github.com/firebase/quickstart-android/tree/master/messaging如何仅在检查CheckBoxPreference时推送Firebase通知?

虽然,我也提供了用户偏好。只有当他们选择通过检查CheckBoxPreference来通知他们时,他们才会收到通知。

我已经成功设置了CheckBoxPreference以及onPreferenceChangeListener。问题是,即使未选中CheckBox,用户也会收到通知。

这是我在SettingsActivity做了:我在上面的代码中有明确规定

public class SettingsActivity extends PreferenceActivity { 

    private AppCompatDelegate mDelegate; 

    Preference myPref; 

    boolean isChecked = true; 

    private static final String TAG = "SettingsActivity"; 

    public static final String RECEIVE_NOTIFS = "receiveNotifications"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     getDelegate().installViewFactory(); 
     getDelegate().onCreate(savedInstanceState); 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.preferences); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     myPref = (CheckBoxPreference) findPreference(RECEIVE_NOTIFS); 
     myPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { 
      @Override 
      public boolean onPreferenceChange(Preference preference, Object o) { 
       isChecked = Boolean.valueOf(o.toString()); 
       if (isChecked) { 
        Toast.makeText(getBaseContext(), "Checked", Toast.LENGTH_SHORT).show(); 
        if (getIntent().getExtras() != null) { 
         String remoteMessage = getIntent().getExtras().getString("remoteMessage"); 
         sendNotification(remoteMessage); 
        } else { 
         Toast.makeText(getBaseContext(), "Error!", Toast.LENGTH_SHORT).show(); 
        } 
       } else { 
        Toast.makeText(getBaseContext(), "Unchecked", Toast.LENGTH_SHORT).show(); 
       } 
       return true; 
      } 
     }); 

    } 

    public void sendNotification(String messageBody) { 
     Intent intent = new Intent(this, SettingsActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle("FCM Message") 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
    } 

CheckBoxPreference检查该通知应该只发送。

这里的MyFirebaseMessagingService.java文件的代码:

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMsgService"; 

    /** 
    * Called when message is received. 
    * 
    * @param remoteMessage Object representing the message received from Firebase Cloud Messaging. 
    */ 
    // [START receive_message] 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     // TODO(developer): Handle FCM messages here. 
     // If the application is in the foreground handle both data and notification messages here. 
     // Also if you intend on generating your own notifications as a result of a received FCM 
     // message, here is where that should be initiated. See sendNotification method below. 
     Log.d(TAG, "From: " + remoteMessage.getFrom()); 
     Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 

     Intent intent = new Intent(this, SettingsActivity.class); 
     intent.putExtra("remoteMessage", remoteMessage.getNotification().getBody()); 
     startActivity(intent); 
    } 
    // [END receive_message] 

} 

我在做什么这里是我送remoteMessage作为意向到SettingsActivitysendNotification()方法有使用它。

因此,显然我想要的是我希望用户只有在他们选择通过检查CheckBoxPreference时才会收到通知。

请让我在这里做错了什么。

+0

你在哪里发送通知?控制台? – Shubhank

+0

@Shubhank我没有得到你。抱歉。请再说一遍。 –

+0

“我正在使用通知来通知用户”,以及如何通知用户。我想使用自定义服务器或Firebase控制台 – Shubhank

回答

2

当您使用Firebase通知时,发送的消息是通知消息。当应用程序处于前台时,您的代码将处理通知。但是,当应用程序背景时,默认通知中心将处理它。由于通知中心对您的复选框一无所知,它将始终显示通知。

看到这个答案的问题在快速启动回购迄今我所见到的最好的解释:https://github.com/firebase/quickstart-android/issues/8#issuecomment-221039175

您有几种选择,让你的逻辑工作:

  • 不要”如果用户没有选中该框,则发送通知。因此,不要禁止显示,只是不要发送它。例如,您可能会发送一个只有已选择订阅的用户的主题。

  • 发送数据信息而不是通知。这确保了您的代码将始终被调用,即使应用程序是后台的。请参阅有关Notifications and data in the message payload的文档以获取有关通知和数据消息之间差异的更多信息。

相关问题:

+0

非常感谢您清除疑问。现在,你能告诉我如何发送数据信息而不是通知吗? 再次感谢! –

+0

请参阅https://firebase.google.com/docs/cloud-messaging/downstream –