2

通知先にボタンを追加する方法FCMボタンにクリックイベントを追加する方法私は却下と回答この画像のように通知FCM Android上でボタンのイベントをクリックして追加しますどのように通知通知時にボタンを追加する方法

上の2つのボタンが必要なAPIレベル4.1のことができますから、

enter image description here

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

これは単なる定期的なアンドロイド通知です。 fcm通知を処理するコードを表示してください –

+0

このリンクをたどることができます:https://stackoverflow.com/questions/21925688/adding-button-action-in-custom-notification –

答えて

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 " + "[email protected]") 
     .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(); 
+0

ボタンのクリックイベントを追加する方法 –

+0

通知ビルダーのaddAction()メソッドに異なる保留アクションを追加します.iは答えを編集しました –

+0

クリックイベントコード –

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

を確認することができ、私は「.addAction」として3つのボタンが追加されました。

NotificationCompat.Action viewAction = new NotificationCompat.Action.Builder(R.drawable.view_icon, "View", viewIntentPending).build(); 
関連する問題