简体   繁体   English

如何在通知FCM Android上添加按钮

[英]How can I add button on notification FCM Android

How I add button on notification FCM and how I add click event on that buttons. 我如何在通知FCM上添加按钮以及如何在该按钮上添加click事件。 I need two buttons on notification 通知我需要两个按钮

How I add click event on button on notification FCM Android like this image Dismiss and Answer 如何在通知FCM Android上的按钮上添加点击事件,如此图像Dismiss and Answer

在此输入图像描述

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "FCM Service";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO: Handle FCM messages here.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
        createNotification(remoteMessage.getNotification().getBody());
    }

    private void createNotification( String messageBody) {
        Intent intent = new Intent( this , ResultActivity. class );
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent, PendingIntent.FLAG_ONE_SHOT);

        Intent intent1 = new Intent(this, ResultActivity.class);
        PendingIntent resultIntents = PendingIntent.getActivity( this , 0, intent1, PendingIntent.FLAG_ONE_SHOT);

        Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Android Tutorial Point FCM Tutorial")
                .setContentText(messageBody)
                .setDefaults(Notification.DEFAULT_ALL)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setAutoCancel(true)
                .addAction(R.drawable.switches, "Hello", resultIntents)
                .addAction(R.drawable.call, "Call", resultIntent)
                .setSound(notificationSoundURI)
                .setContentIntent(resultIntent);
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, mNotificationBuilder.build());
    }
}
public class FirebaseIDService extends FirebaseInstanceIdService {
    private static final String TAG = "FirebaseIDService";

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        // TODO: Implement this method to send any registration to your app's servers.
        sendRegistrationToServer(refreshedToken);
    }

    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
    }
}

From API level 4.1 you can add action buttons to notification. 从API级别4.1,您可以向通知添加操作按钮。 To see the basics about notification check the android doc and for some more help you can check this so answer and this tutorial 要查看有关通知的基础知识,请查看Android文档以及更多帮助,您可以查看此答案和本教程

//Here in intent your will need provide the class which you want to open on button click in notification
Intent intent = new Intent(this, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

// Build notification
// Actions are just fake
Notification noti = new Notification.Builder(this)
        .setContentTitle("New mail from " + "test@gmail.com")
        .setContentText("Subject").setSmallIcon(R.drawable.icon)
        .setContentIntent(pIntent)
        .addAction(R.drawable.icon, "Call", pIntent)
        .addAction(R.drawable.icon, "More", pIntent)
        .addAction(R.drawable.icon, "And more", pIntent).build();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            // Set Icon
            .setSmallIcon(R.drawable.icon)
            // Set Ticker Message
            .setTicker("notification received")
            // Dismiss Notification
            .setAutoCancel(true)
            //.setWhen(0)
            // Set PendingIntent into Notification
            .setContentIntent(pIntent)
            // Set RemoteViews into Notification
            // .setContent(remoteViews)
            // Set Title
            .setContentTitle("App")
            // show big notification hint when app is open
            //.setPriority(Notification.PRIORITY_MAX)
            // Set Text
             .setContentText(messageBody)
            // Set Sound
            .setSound(defaultSoundUri)
            // notification button 1
            .addAction(viewAction)
            // notification button 2
            .addAction(rejectAction)
            // notification button 3
            .addAction(approveAction);

Here I have added 3 buttons as ".addAction". 这里我添加了3个按钮作为“.addAction”。 We can give individual actions for every button it is as follows 我们可以为每个按钮分别进行如下操作

    NotificationCompat.Action viewAction = new NotificationCompat.Action.Builder(R.drawable.view_icon, "View", viewIntentPending).build();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM