【问题标题】:How can I add button on notification FCM Android如何在通知 FCM Android 上添加按钮
【发布时间】:2016-11-10 07:32:49
【问题描述】:

如何在通知 FCM 上添加 按钮 以及如何在这些按钮上添加点击事件。我需要两个通知按钮

我如何在通知 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.
    }
}

【问题讨论】:

标签: android firebase android-notifications firebase-cloud-messaging


【解决方案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();

【讨论】:

  • 我如何在按钮上添加点击事件
  • 您需要在通知生成器的 addAction() 方法上添加不同的待处理操作。我已经编辑了答案
  • 给我一个例子@Burhanuddin Rashid 的点击事件代码
  • 我如何在按钮上添加点击事件@Burhanuddin Rashid
【解决方案2】:
    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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-21
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多