【问题标题】:Enable or disable FCM push notifications by toggle button from application settings通过应用程序设置中的切换按钮启用或禁用 FCM 推送通知
【发布时间】:2017-05-16 14:42:50
【问题描述】:

我在我的应用中通过 Firebase 实现了推送通知。即使从设置中禁用通知,通知也会到来。 我为 Firebase 实现的类是:

    public class MyFirebaseMessagingService extends FirebaseMessagingService {

            private static final String TAG = "MyFirebaseMsgService";
            @Override
            public void onMessageReceived(RemoteMessage remoteMessage) {

                //Displaying data in log

                Log.e(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
                //Calling method to generate notification

                String to="";
                to = remoteMessage.getData().get("key1");

        //when the notification is disabled then also the notification is coming
     if(notification_enable) {
    sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody(),to);
           }
        }

            //This method is only generating push notification
            //It is same as we did in earlier posts
            private void sendNotification(String title,String messageBody,String to) {
                    Intent intent = new Intent(this, Splash_Activity.class);
                    intent.putExtra("key1",to);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);


                PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

                Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.noti_icon)
                        .setContentTitle(title)
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setColor(this.getResources().getColor(R.color.colorAccent))
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

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

                notificationManager.notify(0, notificationBuilder.build());
            }
        }
 public class FirebaseIDService extends FirebaseInstanceIdService {
    private static final String TAG = "FirebaseIDService";

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.e(TAG, "Refreshed token: " + refreshedToken);

        // TODO: Implement this method to send any registration to your app's servers.
        sendRegistrationToServer(refreshedToken);
    }

    /**
     * Persist token to third-party servers.
     *
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
    }
}

并将清单中的类包括为:

<permission
        android:name="com.pixelpoint.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="com.pixelpoint.permission.C2D_MESSAGE" />

<service android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <service android:name=".FirebaseIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>

【问题讨论】:

    标签: android firebase push-notification firebase-cloud-messaging


    【解决方案1】:

    您是否使用 Firebase 控制台发送通知? 如果应用程序在后台并且您的MyFirebaseMessagingService 不会收到回调,这些通知将由系统处理。您的客户端检查用户是否在本地设置中注册以接收通知的代码不适用于所有情况。 (更多关于后台处理的信息在这里https://firebase.google.com/docs/cloud-messaging/android/receive

    我对此的建议是创建一个主题并在注册用户后立即自动为用户订阅该主题:

    FirebaseMessaging.getInstance().subscribeToTopic("news");
    

    然后,当用户关闭通知时,取消订阅主题。

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

    这会将它们从服务器上的列表中删除,并且您不会依赖客户端逻辑来过滤掉不需要的通知。

    然后,当从 Firebase 控制台向客户端发送通知时,您应该仅针对那些为该主题注册的用户

    更多关于主题消息的信息在这里 - https://firebase.google.com/docs/cloud-messaging/android/topic-messaging

    【讨论】:

    • 这是什么(“新闻”)?是强制性的还是非强制性的?
    • @user6789978 这是我正在使用的示例主题,您可以使用与您的应用程序相关的任何主题名称。
    • 但是如果您要发送用户特定的数据,则不建议使用此解决方案,因为这些数据最终可能会发送到多个用户的主题。你如何在不使用主题的情况下处理这个问题?您是否必须在后端创建通知类型并根据用户是否检查(在本地存储/共享首选项中)在客户端过滤每种类型?
    • “如果应用程序在后台,这些通知将由系统处理......”文档说,但这是一个谎言。您的应用将在短时间内被唤醒。您可以通过记录 FirebaseMessagingService 实现的构造函数来验证它。
    猜你喜欢
    • 2019-05-22
    • 1970-01-01
    • 2023-04-06
    • 2022-12-10
    • 2021-12-26
    • 2018-08-21
    • 1970-01-01
    • 2012-05-16
    • 2012-11-28
    相关资源
    最近更新 更多