将 FCM 通知替换为以编程方式创建的通知



我已经切换到为我的应用程序使用 FCM。当我的应用程序打开时,我手动处理消息,如果未显示包含消息列表的Fragment,则我让代码显示Notification。为了显示Notification,我使用该功能:

public void notify(int id, Notification notification)

我遇到的问题是,如果我的应用程序在后台,FCM显示Notification。我什至在服务器上设置了tag参数,因此只会为应用程序显示一个Notification。如果用户打开应用程序而不单击Notification,然后收到一条消息,则会显示一个单独的Notification,这不是我想要的。我切换到使用该功能:

public void notify(String tag, int id, Notification notification)

使用与服务器用于FCM消息相同的tag仍会生成第二个通知。我以编程方式创建的Notification有没有办法替换FCM创建的Notification

请注意

,FCM 处理纯NotificationData有效负载的消息的方式不同。请检查您的消息以了解您发送的消息类型,并查看 Firebase 文档。

我遇到了一个问题,当我正在运行某个活动时,我不想收到通知。因此,我创建了一个静态实用程序类,该类保留了一个布尔属性,指示该活动是否处于活动状态。在我的通知接收器中,我检查此值,并发出或不发出通知。喜欢这个:

我的静态实用程序类:

public class MyRunningActivity {
    private static boolean isActivityRunning = false;
    public static void setIsRunningActivity(boolean isRunning){
        isActivityRunning = isRunning;
    }
    public static boolean getIsRunningActivity(){
        return isActivityRunning;
    }
}

在接收onMessageReceived的类中:

   @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        String notification = "";
        String title = "";
        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            title = getDataWithKey(remoteMessage.getData(), "title");
            notification = getDataWithKey(remoteMessage.getData(), "body");
        }
        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            notification = remoteMessage.getNotification().getBody();
            title = remoteMessage.getNotification().getTitle();
        }
        sendNotification(title, notification);
    }

private void sendNotification(String notificationTitle, String notificationBody) {
    try{
        Intent intent = new Intent(this, MyActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NO_ANIMATION);
        //Generate a new 'unique' request code -- this helps to refresh the activity if it is already active
        int requestCode = CodeGenerator.getRandomNumber(10, 10000);
        PendingIntent pendingIntent = null;
        // If the Activity is active then this will keep the notification from being sent
        // But the intent Extras will be delivered to the OnNewIntent method and handled there.
        // I had to put the singleTop flag in the Manifest, otherwise this will cause the
        // activity to close and reopen....
        try {
            if(MyRunningActivityUtility.getIsRunningActivity()) {
                    intent.putExtra("add_ring_tone","true");
                    pendingIntent = PendingIntent.getActivity(this,
                                              requestCode,
                                              intent,
                                              PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT
                    );
                    pendingIntent.send();
                    \ !!! Notice that return here prevents the Notification from being sent !!!
                    return;
                }
            }
        }
        catch (Exception ex1){
            Log.e(TAG, ex1.getMessage());
        }
        pendingIntent = PendingIntent.getActivity(this,
                                  requestCode,
                                  intent,
                                  PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT
        );

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = (android.support.v7.app.NotificationCompat.Builder)
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.your_custom_icon)
                        .setContentTitle(notificationTitle)
                        .setContentText(notificationBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent)
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        int idNot = CodeGenerator.getRandomNumber(10, 10000);
        notificationManager.notify(idNot, notificationBuilder.build());
    }
    catch (Exception ex){
        Log.e(TAG, ex.getMessage());
    }
}

此示例中可能有一些您不需要的内容。

这是因为

您收到的是 FCM 通知消息:

通知消息在以下情况下传递到通知托盘 应用程序在后台。对于前台应用,消息是 由这些回调处理:

didReceiveRemoteNotification: on iOS
onMessageReceived() on Android.

您应该改为接收 FCM 数据消息,并以编程方式创建通知:

客户端应用负责处理数据消息。

文档

最新更新