android - Firebase 数据通知无法点击
问题描述
嗨,我想在通知上发送图像并使用数据通知这是我的数据,通知带有图片。但未点击通知我如何发送可点击的?
""data"" : {""click_action"":"".MainActivity"",
""body"" : ""new Symulti update 22!"",
""title"" : ""new"",
""url"":""https://www.blablaaas.com"",
""img_url"":""https://www.image.com/image1""
""}}
解决方案
我希望您正在为 android 实现代码,因此要使通知可点击,您只需添加带有目标 Activity 名称的pendingIntent,这样您就可以获得可点击的操作。使用下面的代码作为参考。
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
Intent mainIntent = new Intent(this, TabHostScreen.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent mainPIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setSmallIcon(getNotificationIcon(builder));
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
builder.setAutoCancel(true);
//start intent on notification tap (MainActivity)
builder.setContentIntent(mainPIntent);
//custom style
builder.setStyle(new NotificationCompat.DecoratedCustomViewStyle());
builder.setCustomContentView(remoteCollapsedViews);
//builder.setCustomBigContentView(remoteExpandedViews);
long[] pattern = {500, 500, 500};
builder.setVibrate(pattern);
Random rand = new Random();
NOTIFICATION_ID = rand.nextInt(1000);
//notification manager
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(NOTIFICATION_ID, builder.build());
推荐阅读
- c++ - 我如何禁用 QPushbutton 上的突出显示?
- python - 在 ScrolledText red 中设置特定单词
- javascript - 如何根据内容更改我的 css 溢出?
- perl - 当系统命令中的脚本运行不顺利时,需要退出状态!=0
- c++ - 我将在这段代码中的哪里添加我的删除,我将 new 运算符用于队列变量
- wordpress - 使用 Axios 的 GET 请求随机超时
- apache - 限制浏览某些资源
- python - 比较二元交叉熵损失张量流
- swift - StackView 在 viewDidLayoutSubviews 返回前高度
- sqlite - 从 Firefox 扩展恢复本地存储