自定义通知(Remoteview)在应用被杀死时不起作用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义通知(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,这就是为什么未调用方法的原因。

以上是关于自定义通知(Remoteview)在应用被杀死时不起作用的主要内容,如果未能解决你的问题,请参考以下文章

应用程序被杀死时停止在通知托盘中加载的默认通知?

如何获取所有应用程序的所有通知都运行自定义通知

手机进入睡眠状态时不播放自定义通知声音

Xamarin Android - 自定义通知托盘图标(状态栏)

IOS:应用内通知+通知服务+内容扩展

Appcelerator Titanium 中的自定义通知视图