应用程序终止时,自定义通知(Remoteview)不起作用



应用程序进程终止时,自定义通知不起作用。应用程序在后台/完成时未调用onMessageReceived方法。

如何强制应用程序调用onMessageReceived((,我只想当我从firebase发送通知时,它会显示我的自定义通知。。。

这是代码。。。

public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
configs.notificationId = data.get("notificationId");
configs.title = data.get("title");
configs.subject = data.get("subject");
configs.image = data.get("image");
initwithimage(configs.image);
}
private void initwithimage(String imageUrl) {      
//Code removed for simplified
sendwithimage(bitmap);
}
private void sendwithimage(Bitmap bitmap) {
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_layout);
contentView.setImageViewBitmap(R.id.image, bitmap);
contentView.setTextViewText(R.id.title, configs.title);
contentView.setTextViewText(R.id.subject, configs.subject);
contentView.setTextViewText(R.id.timeago, DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME));
Intent openit = new Intent(this, notificationintentservice.class);
openit.setAction("openit");
contentView.setOnClickPendingIntent(R.id.openit, PendingIntent.getService(this, 0, openit, PendingIntent.FLAG_UPDATE_CURRENT));
Intent closeit = new Intent(this, notificationintentservice.class);
closeit.setAction("closeit");
contentView.setOnClickPendingIntent(R.id.closeit, PendingIntent.getService(this, 0, closeit, PendingIntent.FLAG_UPDATE_CURRENT));
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent,0);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "101";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
@SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_MAX);
//Configure Notification Channel
notificationChannel.setDescription("Epic Studio");
notificationChannel.enableLights(true);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContent(contentView)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setAutoCancel(true)
.setSound(defaultSound)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_MAX);
assert notificationManager != null;
notificationManager.notify(Integer.parseInt(configs.notificationId), notificationBuilder.build());
}
@Override
public void onNewToken(@NonNull String token) {}

}

我知道firebase不支持数据类型通知,为此我们使用Postman/ARC这样的通知客户端,但我不知道如何使用它们。我试过Postman和ARC,但自定义通知仍然不起作用。

这里是JSON

{
"to" : "bLawblawblaw-FeR06mmyABLaBlawblawblawblawblaw",
"collapse_key" : "type_a",
"notification" : {
"body" : "Body of Your Notification",
"title": "Title of Your Notification"
},
"data" : {
"body" : "Body of Your Notification in Data",
"title": "Title of Your Notification in Title",
"key_1" : "Value for key_1",
"key_2" : "Value for key_2"
}
}

我还尝试了下面的JSON,只用于发送数据,但不起作用。通知未到达

{
"to" : "bLawblawblaw-FeR06mmyABLaBlawblawblawblawblaw",
"data" : {
"body" : "Body of Your Notification in Data",
"title": "Title of Your Notification in Title",
"key_1" : "Value for key_1",
"key_2" : "Value for key_2"
}
}

您正在发送数据通知,因此首先必须获取发送通知数据,然后在数据对象中获取值

Map<String, String> data = remoteMessage.getData().get("data");

同时在数据对象中添加notificationId主题图像密钥其他方面的通知将不会显示在通知堆栈中,因为您将不会发现密钥异常

问题是onMessageReceived上的@NonNull(@NonNull……(

我通过客户端在我的应用程序中发送数据通知,所以没有消息通知意味着RemoteMessage为null,这就是为什么方法没有被调用的原因。

相关内容

  • 没有找到相关文章