如何在推送通知中添加点击事件?
Posted
技术标签:
【中文标题】如何在推送通知中添加点击事件?【英文标题】:How to add click event in push notification? 【发布时间】:2020-07-15 18:38:06 【问题描述】:我尝试在推送通知中添加click event
,但每次都收到错误消息。没有click event
,它工作正常。它按时完美地显示通知。我尝试使用Intent
来做到这一点。
这是这样做的方法吗?如何在通知本身上实现click event
?
这是我目前拥有的:
public class MyFirebaseInstanceService extends FirebaseMessagingService
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage)
super.onMessageReceived(remoteMessage);
if (remoteMessage.getData().isEmpty())
showNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
else
showNotification(remoteMessage.getData());
private void showNotification(Map<String,String> data)
String title=data.get("title").toString();
String body=data.get("body").toString();
NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID="example.mfree.services.test";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
NotificationChannel notificationChannel=new NotificationChannel(NOTIFICATION_CHANNEL_ID,"Notification",
NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("Dipu");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.enableLights(true);
notificationManager.createNotificationChannel(notificationChannel);
NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).
setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable.ic_money)
.setContentTitle(title)
.setContentText(body)
.setContentInfo("Info");
notificationManager.notify(new Random().nextInt(),notificationBuilder.build());
private void showNotification(String title,String body)
NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID="com.example.mfree.services.test";
Intent intent = new Intent(getApplicationContext(), Notification_send_Activity.class);
startActivity(intent);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
NotificationChannel notificationChannel=new NotificationChannel(NOTIFICATION_CHANNEL_ID,"Notification",
NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("Dipu");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.enableLights(true);
notificationManager.createNotificationChannel(notificationChannel);
NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).
setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable.ic_money)
.setContentTitle(title)
.setContentText(body)
.setContentInfo("Info");
notificationManager.notify(new Random().nextInt(),notificationBuilder.build());
@Override
public void onNewToken(@NonNull String s)
super.onNewToken(s);
Log.d("TOKENFIREBASE",s);
【问题讨论】:
错误是什么?请将其添加到您的问题中。 您能否详细解释一下您是如何添加“点击事件”的?另外,单击通知您真正想做什么?请注意,通知只能接受PendingIntent
作为点击操作。另请阅读Set the notification's tap action
【参考方案1】:
您需要添加一个action
Notification.Action action = new NotificationCompat.Action(icon, title, pendingIntent);
Notification notification = new NotificationCompat.Builder(context)
.addAction(action)
.build();
【讨论】:
以上是关于如何在推送通知中添加点击事件?的主要内容,如果未能解决你的问题,请参考以下文章
如何从 WatchOs 中的推送通知中获取数据并处理推送通知操作按钮单击事件