FCM - 在 onMessageReceived 中设置徽章 [英] FCM - Setting badge in onMessageReceived

查看:18
本文介绍了FCM - 在 onMessageReceived 中设置徽章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Android 应用程序,我正在使用某种方法在应用程序图标上显示通知编号.现在我想在收到通知时设置那个数字.

I have an Android application, where I'm using some method to show notification number on app icon. Now I want to set that number when notification is received.

我认为我应该在收到通知时设置数字,所以我将它设置在 onMessageReceived 方法中.但是,我的问题是当我的应用在后台时,没有调用 onMessageReceived 方法,所以没有设置通知编号.

I thought that I should set the number when notification received so I set it inside onMessageReceived method. But, my problem is when my app is in background, onMessageReceived method not called, so the notification number isn't set.

以下是我的代码.我在 onMessageReceived 中设置了数字.我已经测试了 setBadge 方法并且可以验证它是否正常工作.问题是 onMessageReceived 没有被调用,所以 setBadge 也没有被调用,它没有设置数字.

Following is my code. I set the number inside onMessageReceived. I already tested setBadge method and can verify that it is working. The problem is onMessageReceived is not called so setBadge is also not called, which doesn't set the number.

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    // TODO(developer): Handle FCM messages here.
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Conts.notificationCounter ++;
    //I am setting in here.
    setBadge(getApplicationContext(),Conts.notificationCounter  );
    Log.e("notificationNUmber",":"+ Conts.notificationCounter);

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}
// [END receive_message]



public static void setBadge(Context context, int count) {
    String launcherClassName = getLauncherClassName(context);
    if (launcherClassName == null) {
        Log.e("classname","null");
        return;
    }
    Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
    intent.putExtra("badge_count", count);
    intent.putExtra("badge_count_package_name", context.getPackageName());
    intent.putExtra("badge_count_class_name", launcherClassName);
    context.sendBroadcast(intent);
}

public static String getLauncherClassName(Context context) {

    PackageManager pm = context.getPackageManager();

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
    for (ResolveInfo resolveInfo : resolveInfos) {
        String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
        if (pkgName.equalsIgnoreCase(context.getPackageName())) {
            String className = resolveInfo.activityInfo.name;
            return className;
        }
    }
    return null;
}

当我搜索此问题,我发现如果即将到来的消息是显示消息,那么只有在应用程序处于前台时才调用onMessageReceived.但是如果收到的消息是数据消息,那么即使应用程序是后台,也会调用onMessageReceived.

When I searched this issue, I found that if the coming message is display message then onMessageReceived is called only if app is foreground. But if coming message is data message then onMessageReceived is called even if the app is background.

但是我的朋友告诉我谁在发送通知(服务器端),消息已经作为显示和数据消息发送.他说数据对象被填充了.

But my friend told me who is sending the notification(server side), the message already goes as both display and data message. He said that data object is filled.

以下是即将到来的消息JSON,它有数据对象.

{  
   "to":"my_device_id",
   "priority":"high",

   "notification":{  
      "body":"Notification Body",
      "title":"Notification Title",
      "icon":"myicon",
      "sound":"default"
   },

   "data":{  
      "Nick":"DataNick",
      "Room":"DataRoom"
   }
}

如果我只使用数据对象,则按他们所说的那样调用 onMessageReceived,但该时间通知不会出现在顶部.

If I only use data object, onMessageReceived is called as they said but that time notification does not appear at the top.

如果消息也是数据消息,为什么不调用 onMessageReceived.我应该做一些不同的事情来处理数据消息吗?它是否与客户端的显示消息相同.

Now why onMessageReceived is not called if the message is also data message. Should I do something different to handle data message? Is it working same with display messaging in client side.

任何帮助将不胜感激.提前致谢.

Any help would be appreciated. Thanks in advance.

推荐答案

没有办法调用 onMessageReceived 除非即将到来的 json 包含 ONLY 数据负载,正如我从 Firebase 支持中了解到的那样.

No way to call onMessageReceived unless the coming json includes ONLY data payload as I learned from Firebase support.

所以我必须使用数据负载,但如果您使用数据负载,它不会在顶部显示通知,因此您应该使用数据负载信息创建自定义通知.

So I have to use data payload but if you use data payload it does not show notification at the top so you should create your custom notification using data payload information.

所以当我在 onMessageReceived 中获得数据负载时,我向自己发送了通知.我在向自己发送通知后立即在 onMessageReceived 中设置了徽章.

So I sent notification to myself when I get the data payload in onMessageReceived. And I set the badge in onMessageReceived right after sending notification to myself.

以下代码为最终版本.

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    //for data payload
    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {

        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        title = remoteMessage.getData().get("title");
        sendNotification(remoteMessage.getData().get("body"), title);
        badge = Integer.parseInt(remoteMessage.getData().get("badge"));
        Log.e("notificationNUmber",":"+badge);
        setBadge(getApplicationContext(), badge);

    }
    //for notification payload so I did not use here
    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {

        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}
// [END receive_message]

private void sendNotification(String messageBody, String title) {
    Intent intent = new Intent(this, MainMenuActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, notify_no /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);
    if (notify_no < 9) {
        notify_no = notify_no + 1;
    } else {
        notify_no = 0;
    }
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher_3_web)
            .setContentTitle(title)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

    notificationManager.notify(notify_no + 2 /* ID of notification */, notificationBuilder.build());
}

谢谢大家.

这篇关于FCM - 在 onMessageReceived 中设置徽章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆