用编程创建的通知替换FCM通知

3

我已经开始使用 FCM 来开发我的应用。当我的应用程序处于打开状态时,我会手动处理消息。如果包含消息列表的 Fragment 没有被显示出来,那么我会让代码显示一个 Notification。为了显示这个 Notification,我使用以下函数:

public void notify(int id, Notification notification)

我遇到的问题是当我的应用程序在后台运行时,FCM 会显示一个通知。我甚至在服务器上设置了参数,以便应用程序只显示一个通知。但如果用户打开应用程序而没有点击通知,然后接收到消息,会显示一个单独的通知,这不是我想要的。我切换到使用以下功能:
public void notify(String tag, int id, Notification notification)

如果我使用与服务器用于FCM消息相同的tag,仍会产生第二个通知。是否有一种程序化创建的Notification可以替换FCM创建的Notification

2个回答

1
请注意,FCM以不同的方式处理纯通知或数据负载的消息。请检查您发送的消息以查看其类型,并查阅Firebase文档。
当我有某个特定的Activity正在运行时,我不想收到通知。因此,我创建了一个静态实用程序类,它保留一个布尔属性,指示该Activity是否处于活动状态。在我的通知接收器中,我检查这个值并发出通知或不发出通知。就像这样:
我的静态实用程序类:
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());
    }
}

这个例子中可能有一些你不需要的东西。

0

这是因为您正在接收 FCM 通知消息:

当应用程序在后台运行时,通知消息将传递到通知托盘。对于前台应用程序,消息由以下回调处理:

iOS 上的 didReceiveRemoteNotification:
Android 上的 onMessageReceived()。

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

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

文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接