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

4

我如何在FCM通知上添加按钮并在按钮上添加点击事件?我需要在通知中添加两个按钮。

我如何在FCM Android通知上为按钮添加点击事件,就像这张图片上的Dismiss和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.
    }
}

1
这只是一个普通的Android通知。请展示你的代码来处理FCM通知。 - OneCricketeer
请参考以下链接:https://dev59.com/fWEh5IYBdhLWcg3w3muK 添加自定义通知中的按钮动作 - Hardik Mehta
2个回答

1
从API 4.1开始,您可以在通知中添加操作按钮。要了解有关通知的基础知识,请查看android doc,如果需要更多帮助,可以查看此so answer和此tutorial
//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();

如何在按钮上添加点击事件 - Kishore Tendulkar
你需要在通知构建器的addAction()方法中添加两个不同的待处理操作...我已经编辑了答案。 - Burhanuddin Rashid
请给我提供一个Burhanuddin Rashid的点击事件代码示例。 - Kishore Tendulkar
如何在这些按钮上添加点击事件 @Burhanuddin Rashid - Kishore Tendulkar
@BurhanuddinRashid 我们能否在使用 FCM 时,在通知中添加一个动作按钮并指定该按钮的操作? - Rohit Bandil
显示剩余2条评论

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

我在这里添加了3个按钮,使用".addAction"命令。我们可以为每个按钮设置单独的操作,如下所示。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接