简体   繁体   English

如何在推送通知中添加点击事件?

[英]How to add click event in push notification?

I have tried to add a click event onto the push notification but I get an error every time.我试图在推送通知中添加一个click event ,但每次都会出错。 Without the click event , it works fine.没有click event ,它工作正常。 It is showing the notification perfectly on time.它按时完美地显示通知。 I have tried to do this with an Intent .我试图用Intent来做到这一点。

Is this the way to do it?这是这样做的方法吗?
How do I implement a click event on the notification itself?如何在通知本身上实现click event

Here is what I currently have:这是我目前拥有的:

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);
    }
}

You need to add an action你需要添加一个动作

Notification.Action action = new NotificationCompat.Action(icon, title, pendingIntent);

Notification notification = new NotificationCompat.Builder(context)
   .addAction(action)
   .build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在单击推送通知时打开特定活动 - How to open specific Activity on Click of Push Notification 单击通知选项卡中的推送通知消息,如何打开特定片段? - How to open the particular fragment on the click of the push notification message in the notification tab? 如何在JavaPNS推送通知中向有效负载添加JSON? - How to add a JSON to payload in JavaPNS push notification? 如何在ViewFlipper中添加单击事件 - How to add a click event in ViewFlipper 当应用程序无法在后台运行时,如何在点击推送通知中打开网址 - how to open url on click push notification when app is not working in background 如何通过推送通知使用Android Parse触发事件? - How to trigger an event using Parse for Android via push notification? Android(PhoneGap)-通过推送通知中的按钮单击事件发送HTTP请求 - Android (PhoneGap) - Send HTTP request from a button's click event in Push Notification 如何在我自己的Android应用程序中添加推送通知 - How to add a push notification in my own android app 如何向WebView应用程序添加推送通知(Android和iOS) - How to add push notification to WebView App (both android and iOS) 如何使用驱动器推送通知从DRIVE API获取通知的事件名称? - How to get event name of notification from DRIVE API using drive push notification?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM