【问题标题】:enable and disable firebase notification using switch buttons使用开关按钮启用和禁用 Firebase 通知
【发布时间】:2019-09-05 14:48:04
【问题描述】:

使用开关按钮启用和禁用 Firebase 通知。?我使用后端的 php 代码获取 fcm 令牌并将其放入数据库(phpmyadmin)中。然后我向应用程序生成推送通知。通知正确发出,但我无法阻止它。我想使用开关按钮启用和禁用通知。

public class FcmMsgService  extends FirebaseMessagingService {
    private static final String NOTIFICATION_CHANNEL_ID1 = "channel_id" ;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage)
    {

        String title = remoteMessage.getNotification().getTitle();
        String message = remoteMessage.getNotification().getBody();
        String click_action = remoteMessage.getNotification().getClickAction();
        Intent intent = new Intent(click_action);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID1);
        builder.setContentTitle(title);
        builder.setContentText(message);
        builder.setSmallIcon(R.drawable.ic_notifications);
        builder.setAutoCancel(true);
        builder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());
        // super.onMessageReceived(remoteMessage);
    }
}

主要活动

public class MainActivity extends AppCompatActivity {

    Switch switchBtn;
    SharedPreferences.Editor prefEditor;
    SharedPreferences prefs;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sendBtn= findViewById(R.id.send_token);
        prefEditor= PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
        prefs=PreferenceManager.getDefaultSharedPreferences(getBaseContext());

        switchBtn= findViewById(R.id.switch2);

            switchBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                    if (switchBtn.isChecked()) 
                          {

          // when i user enable button the notification can allowed to come
                        prefEditor.putString("checked","yes");
                        prefEditor.apply();

                          }
                    else
                     {
         // when i user disable button the notification cannot allowed to come
                        //how to Stop the firebase notification?????
                        prefEditor.putString("checked","no");
                        prefEditor.apply();
                   }}
```[enter image description here][1]


  [1]: https://i.stack.imgur.com/RY5yp.png

【问题讨论】:

  • 解决方案取决于场景:您是根据设备令牌向特定用户发送通知还是根据FirebaseMessaging topic?发送通知

标签: android firebase firebase-cloud-messaging


【解决方案1】:

也许尝试取消订阅每个主题?

FirebaseMessaging.getInstance().unsubscribeFromTopic("YourTopic");

另外,这段代码可以简化:

switchBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    prefEditor.putBoolean("checked", switchBtn.isChecked()).apply();
            }
}

【讨论】:

    【解决方案2】:

    通过主题启用通知:

    FirebaseMessaging.getInstance()
                .subscribeToTopic(yourTopic);
    

    然后禁用:

    FirebaseMessaging.getInstance().unsubscribeFromTopic(yourTopic);
    

    在您的代码中:

    if (switchBtn.isChecked()) {
      // Subscribe here
      FirebaseMessaging.getInstance().subscribeToTopic(yourTopic);
      prefEditor.putString("checked","yes");
      prefEditor.apply();
    }
    else {
      // Subscribe here
      FirebaseMessaging.getInstance().unsubscribeFromTopic(yourTopic);
      prefEditor.putString("checked","no");
      prefEditor.apply();
    }
    

    另外,由于这个函数返回一个任务,你可以这样做:

    TopicManagementResponse response = FirebaseMessaging.getInstance().unsubscribeFromTopic(
        registrationTokens, topic);
    // See the TopicManagementResponse reference documentation
    // for the contents of response.
    System.out.println(response.getSuccessCount() + " tokens were unsubscribed successfully");
    

    【讨论】:

    • 对不起,我没听懂。
    • 请看下面mainactivity中的php代码图片。
    猜你喜欢
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多