android - 带有 PendingIntent 和 putExtra 的 Java 通知不起作用
问题描述
我有一条 FireBase 消息。如果它进来,我会显示一个通知(作品)。当我单击通知时,它应该打开我的应用程序(工作)。现在我想使用 putExtra() 将一些信息交给我的应用程序(仅有时有效 - 如果应用程序位于前台)。那是我的问题。
private void sendMyNotification(String message) {
int rndNo4Intent = Math.round((float)(Math.random()*100000000));
int rndNo4Msg = Math.round((float)(Math.random()*100000000));
//On click of notification it redirect to this Activity
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("action","openWiki"+rndNo4Msg); // My Test-Data
PendingIntent pendingIntent = PendingIntent.getActivity(this, rndNo4Intent , intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String channelId = getString(R.string.default_notification_channel_id);
Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("CodingSpace")
.setContentText(message)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
// Since android Oreo notification channel is needed. Sonst kommt nichts an.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"CodingSpaceChannel",
NotificationManager.IMPORTANCE_HIGH); // in Oreo ist High so, dass es einen Ton gibt
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(rndNo4Msg, notificationBuilder.build());
}
在 .MainActivity 类中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
Intent iin= getIntent();
String msg = iin.getStringExtra("action");
Toast.makeText(MainActivity.this,"get: "+ msg,Toast.LENGTH_SHORT).show();
解决方案
推荐阅读
- javascript - 带有渲染组件的 React Router 层次结构
- python - discord.py:如何绕过错误 BadArgument?
- mysql - MySQL 从两个表中除法计算
- javascript - 如果缺少环境变量,则 Gatsby 构建失败
- html - 在 mat-grid-list 中使用 rowspan 和 colspan 元素未正确显示
- python - 模块 tensorflow 没有属性 contrib
- python - 使用 python 从单个 youtube 播放列表链接中提取单个链接
- c++ - 如何读取ORC文件列数据
- java - 如何将菜单项与列表视图绑定
- wordpress - 按下提交按钮几秒钟后自动刷新