android - 应用程序终止时自定义通知(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"
}
}
解决方案
问题是 onMessageReceived 处的@NonNull( @NonNull ....)
我正在通过客户端在我的应用程序中发送数据通知,因此没有消息通知,这意味着 RemoteMessage 为空,这就是未调用方法的原因。
推荐阅读
- kubernetes - 将链码实例化到 Hyperledger Fabric 上的不同通道上
- swiftui - SwiftUI 2.0:如何锁定屏幕方向?
- asynchronous - 在异步函数的匹配中出现死锁
- r - dplyr 中的 mutate_at 出错-按组尝试窗口化滞后
- c# - 无法对 BlobServiceClient 和 Azure AD 进行身份验证
- javascript - iOS Safari:点击结束后点击突出显示(-webkit-tap-highlight)卡在屏幕上
- sequelize.js - 在 sequelize 中迁移和播种后未创建第二组表
- database - PLS-00201:使用 EXECUTE IMMEDIATE 时必须声明标识符“IMMEDIATE”?
- netlogo - NetLogo 中的基本问题(创建等式)
- javascript - 赛普拉斯 - Salesforce | 在同一测试场景中运行第二次尝试后,我们遇到了一个问题