首页 > 解决方案 > 应用程序终止时自定义通知(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"
 }
}

标签: androidandroid-studionotificationsfirebase-cloud-messaging

解决方案


问题是 onMessageReceived 处的@NonNull( @NonNull ....)

我正在通过客户端在我的应用程序中发送数据通知,因此没有消息通知,这意味着 RemoteMessage 为空,这就是未调用方法的原因。


推荐阅读