当应用程序处于前台或后台时,如何使用FCM处理通知

Leo*_*ang 5 notifications android firebase firebase-cloud-messaging

我使用firebase来构建我的项目.
它还将使用FCM(firebase云消息).
但有一个问题.
当app在后台时,我无法处理FCM(创建我的自定义通知).

官方网站教程
案例1:应用前台 - >覆盖"onMessageReceived()"来创建自定义通知.
案例2:应用程序背景 - >系统将直接创建通知.我们不需要也不能做任何事情.因为在这种情况下它不会触发"onMessageReceived()".

但是,如果我在应用程序是后台时无法执行任何操作,则无法创建自定义通知.(例如,在用户单击通知后,它将弹出一个窗口以显示详细信息.)

那么,当应用程序处于后台时,如何使用FCM处理通知?

Leo*_*ang 7

有一个坏消息.
Google在"com.google.firebase:firebase-messaging:11.6.0"版本中更改了Firebase源代码.
handelIntent现在是"公共最终无效方法".这意味着我们无法覆盖它.
如果您要使用该解决方案,请将版本更改为"com.google.firebase:firebase-messaging:11.4.2"



试试我的方式.它可以完美地在项目构建版本上面安装Android 6.0(api level 23),我已经尝试过了.

有比官方网站教程更好的方法

官方网站表示,当应用程序处于后台时,系统会创建通知.因此,您无法通过覆盖"onMessageReceived()"来处理它.因为"onMessageReceived()"仅在app处于前台时触发.

但事实并非如此.实际上,通知(当应用程序在后台时)是由Firebase库创建的.

在我跟踪了firebase库代码之后.我找到了更好的方法.

步骤1.在FirebaseMessagingService中覆盖"handleIntent()"而不是"onMessageReceived()"
原因:
因为该方法将触发app在前台或后台.因此,我们可以处理FCM消息并在两种情况下创建自定义通知.

@Override
public void handleIntent(Intent intent) {
    Log.d( "FCM", "handleIntent ");
}
Run Code Online (Sandbox Code Playgroud)


步骤2.解析来自FCM的消息
如何:
如果您不知道您设置的消息的格式.打印并尝试解析它.
这是基本的插图

Bundle bundle = intent.getExtras();
if (bundle != null) {
    for (String key : bundle.keySet()) {
        Object value = bundle.get(key);
        Log.d("FCM", "Key: " + key + " Value: " + value);
    }
}
Run Code Online (Sandbox Code Playgroud)


步骤2.当应用程序处于后台时,删除Firebase库创建的通知
原因:
我们可以创建自定义通知.但Firebase库创建的通知仍然存在(实际上它是由""super.handleIntent(intent)""创建的.下面有详细说明.).然后我们将有两个通知.这很奇怪.因此,我们必须删除Firebase库创建的通知

(项目构建级别是上面的Android 6.0):
识别我们要删除的通知并获取信息.并使用"notificationManager.cancel()"删除它们.

private void removeFirebaseOrigianlNotificaitons() {

    //check notificationManager is available
    NotificationManager notificationManager = 
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (notificationManager == null )
        return;

    //check api level for getActiveNotifications()
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        //if your Build version is less than android 6.0
        //we can remove all notifications instead. 
        //notificationManager.cancelAll();
        return;
    }


    //check there are notifications
    StatusBarNotification[] activeNotifications = 
        notificationManager.getActiveNotifications();
    if (activeNotifications == null)
        return;

    //remove all notification created by library(super.handleIntent(intent))
    for (StatusBarNotification tmp : activeNotifications) {
        Log.d("FCM StatusBarNotification", 
            "StatusBarNotification tag/id: " + tmp.getTag() + " / " + tmp.getId());
        String tag = tmp.getTag();
        int id = tmp.getId();

        //trace the library source code, follow the rule to remove it.
        if (tag != null && tag.contains("FCM-Notification"))
            notificationManager.cancel(tag, id);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的整个示例代码:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static int notificationCount=0;

@Override
public void handleIntent(Intent intent) {
    //add a log, and you'll see the method will be triggered all the time (both foreground and background).
    Log.d( "FCM", "handleIntent");

    //if you don't know the format of your FCM message,
    //just print it out, and you'll know how to parse it
    Bundle bundle = intent.getExtras();
    if (bundle != null) {
        for (String key : bundle.keySet()) {
            Object value = bundle.get(key);
            Log.d("FCM", "Key: " + key + " Value: " + value);
        }
    }

    //the background notification is created by super method
    //but you can't remove the super method. 
    //the super method do other things, not just creating the notification
    super.handleIntent(intent);

    //remove the Notificaitons
    removeFirebaseOrigianlNotificaitons();

    if (bundle ==null)
        return;

    //pares the message
    CloudMsg cloudMsg = parseCloudMsg(bundle);

    //if you want take the data to Activity, set it
    Bundle myBundle = new Bundle();
    myBundle.putSerializable(TYPE_FCM_PLATFORM, cloudMsg);
    Intent myIntent = new Intent(this, NotificationActivity.class);
    myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    myIntent.putExtras(myBundle);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationCount, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    //set the Notification
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.icon)
            .setContentTitle(cloudMsg.getTitle())
            .setContentText(cloudMsg.getMessage())
            .setAutoCancel(true)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(notificationCount++, notificationBuilder.build());
}



/**
 * parse the message which is from FCM
 * @param bundle
 */
private CloudMsg parseCloudMsg(Bundle bundle) {
    String title = null, msg=null;

    //if the message is sent from Firebase platform, the key will be that
    msg = (String) bundle.get("gcm.notification.body");

    if(bundle.containsKey("gcm.notification.title"))
    title = (String) bundle.get("gcm.notification.title");

    //parse your custom message
    String testValue=null;
    testValue =  (String) bundle.get("testKey");

    //package them into a object(CloudMsg is your own structure), it is easy to send to Activity.
    CloudMsg cloudMsg = new CloudMsg(title, msg, testValue);
    return cloudMsg;
}


/**
 * remove the notification created by "super.handleIntent(intent)"
 */
    private void removeFirebaseOrigianlNotificaitons() {

    //check notificationManager is available
    NotificationManager notificationManager = 
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (notificationManager == null )
        return;

    //check api level for getActiveNotifications()
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        //if your Build version is less than android 6.0
        //we can remove all notifications instead. 
        //notificationManager.cancelAll();
        return;
     }

    //check there are notifications
    StatusBarNotification[] activeNotifications = 
        notificationManager.getActiveNotifications();
    if (activeNotifications == null)
        return;

    //remove all notification created by library(super.handleIntent(intent))
    for (StatusBarNotification tmp : activeNotifications) {
        Log.d("FCM StatusBarNotification", 
            "tag/id: " + tmp.getTag() + " / " + tmp.getId());
        String tag = tmp.getTag();
        int id = tmp.getId();

        //trace the library source code, follow the rule to remove it.
        if (tag != null && tag.contains("FCM-Notification"))
            notificationManager.cancel(tag, id);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

  • 这个解决方案不好。如果您的应用程序未在运行,您将不会收到伤寒记录。这就是Google从api中删除它的原因,不需要它。正确的方法是使用Firebase通知(如下所述)或同步适配器/后台服务。 (3认同)