android - 我正在向所有用户发送通知 ..用户正在收到通知,但单击通知时未开始活动
问题描述
我通过 FCM 从我的管理应用程序向所有用户发送通知,用户收到通知但无法在单击通知时打开活动。这是我的接收通知
private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.icon_large);
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle bundle = new Bundle();
bundle.putString("picture_url", data.get("picture_url"));
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
String CHANNEL_ID="01";
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,CHANNEL_ID)
.setContentTitle(notification.getTitle())
.setContentText(notification.getBody())
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
//.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.win))
.setContentIntent(pendingIntent)
.setContentInfo("Hello")
.setLargeIcon(icon)
.setColor(getResources().getColor(R.color.theme))
.setLights(Color.RED, 1000, 300)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setSmallIcon(R.drawable.icon_small);
try {
String picture_url = data.get("picture_url");
if (picture_url != null && !"".equals(picture_url)) {
URL url = new URL(picture_url);
Bitmap bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream());
notificationBuilder.setStyle(
new NotificationCompat.BigPictureStyle().bigPicture(bigPicture).setSummaryText(notification.getBody())
);
}
} catch (IOException e) {
e.printStackTrace();
}
NotificationManager notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//CharSequence name = getString(R.string.channel_name);
String name="Channel_001";
String description="Channel Description";
//String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
//NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
//NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
该操作没有任何错误,单击通知时,通知消失,但它没有打开意图中提供的活动。助手请注意= Mainactivity.java 是我项目中的 LAUNCHER 活动。
解决方案
Override
在你的下面MainActivity
@Override
public void onNewIntent(Intent intent) {
setIntent(intent);
super.onNewIntent(intent);
}
推荐阅读
- tsql - IF...ELSE 子句的语法不正确
- php - 如何从其他数组中获取多维数组
- firebase - Firebase 云功能总是在日志中超时
- javascript - Python Flask-socketio 工作,没有从 JS 客户端接收数据
- selenium - 在 Seelnium 网格中的 Jenkins 上运行 Serenity 测试时出现异常
- swift - 返回行数时,Swift中的“self”需要什么
- git - 使用不需要的未跟踪文件进行变基。合并错误
- mysql - 在 Spring Boot & Hibernate 应用程序中使用 XtraDB 作为存储引擎
- python - Python 3.6 (PyCharm) 通过代理错误连接
- geocoding - 谷歌批量地理编码 API