Hil*_*lal 8 android google-cloud-messaging firebase-cloud-messaging firebase-notifications
我有一个Android应用程序,我正在使用一些方法在应用程序图标上显示通知编号.现在我想在收到通知时设置该号码.
我认为我应该在收到通知时设置数字,所以我在onMessageReceived
方法中设置它.但是,我的问题是当我的应用程序处于后台时,onMessageReceived
方法未被调用,因此未设置通知编号.
以下是我的代码.我在里面设置了号码onMessageReceived
.我已经测试过setBadge
方法并且可以验证它是否正常工作.问题是onMessageReceived
没有调用所以setBadge
也没有调用,没有设置数字.
@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;
}
Run Code Online (Sandbox Code Playgroud)
当我搜索这个问题时,我发现如果即将发出的消息是显示消息,那么onMessageReceived
只有当app是前景时才会调用它.但是,如果即将发送消息是数据消息,则onMessageReceived
即使应用程序是后台也会调用.
但是我的朋友告诉我发送通知的人(服务器端),消息已经同时显示和数据消息.他说数据对象已经填满了.
以下是JSON用于未来的消息,它的数据对象.
{
"to":"my_device_id",
"priority":"high",
"notification":{
"body":"Notification Body",
"title":"Notification Title",
"icon":"myicon",
"sound":"default"
},
"data":{
"Nick":"DataNick",
"Room":"DataRoom"
}
}
Run Code Online (Sandbox Code Playgroud)
如果我只使用数据对象,则按照他们的说法调用onMessageReceived,但该时间通知不会出现在顶部.
现在,onMessageReceived
如果消息也是数据消息,为什么不调用.我应该做些不同的事情来处理数据信息吗?它是否与客户端的显示消息相同.
任何帮助,将不胜感激.提前致谢.
没有办法调用onMessageReceived,除非即将到来的json 仅包含数据有效负载,这是我从Firebase支持中学到的.
所以我必须使用数据有效负载,但如果您使用数据有效负载,它不会在顶部显示通知,因此您应该使用数据有效负载信息创建自定义通知.
因此,当我在onMessageReceived中获取数据有效负载时,我向自己发送了通知.我在向自己发送通知后立即在onMessageReceived中设置徽章.
以下代码是最终版本.
@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());
}
Run Code Online (Sandbox Code Playgroud)
谢谢大家.
归档时间: |
|
查看次数: |
18959 次 |
最近记录: |