0

アプリがバックグラウンドのときに受信トレイスタイルでfcm通知を追加するにはどうすればよいですか?fcm通知を追加するには?

私はアプリが

オープンですが、アプリがバックグラウンドであれば、それは受信トレイのスタイルを追加する方法を別々の通知に

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMsgService"; 

    Integer notify_no = 0; 

    Integer numMessages = 0; 

    DBHelper db = new DBHelper(this); 

    private final int notificationID = 237; 
    private static int value = 0; 
    // Notification.InboxStyle inboxStyle = new Notification.InboxStyle(); 
    //Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.newlogo); 


     // TODO(developer): Handle FCM messages here. 

     Log.d(TAG, "From: " + remoteMessage.getFrom()); 

     // Check if message contains a data payload. 
     if (remoteMessage.getData().size() > 0) { 
      Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
      /* Integer badge = Integer.parseInt(remoteMessage.getData().get("badge")); 
      Log.d("notificationNUmber",":"+badge); 
      setBadge(getApplicationContext(), badge);*/ 
     } 

     // Check if message contains a notification payload. 
     if (remoteMessage.getNotification() != null) { 
      Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
     } 



     Intent intent = new Intent(); 
     intent.setAction("com.ksoft.propreka.CUSTOM_INTENT"); 
     sendBroadcast(intent); 

     db.insertNotification(remoteMessage.getNotification().getBody(),remoteMessage.getData().get("room_id")); 

     //EventBus.getDefault().post(remoteMessage.getNotification().getBody()); 
     // 

     try { 
      if (remoteMessage.getNotification() != null) { 
       sendNotification(remoteMessage.getData().get("text")); 
      } else if (!remoteMessage.getData().isEmpty()) { 
       sendNotification(remoteMessage.getData().get("text")); 
      } 
     } catch (Exception e) { 
      Log.d("json error", e.toString()); 
     } 

     //sendNotification(remoteMessage.getData().get("text")); 
     Log.d("test",":test notification"); 
     //createpushnotification(); 

     // Also if you intend on generating your own notifications as a result of a received FCM 
     // message, here is where that should be initiated. See sendNotification method below. 
    } 

    /** 
    * Create and show a simple notification containing the received FCM message. 
    * 
    * @param messageBody FCM message body received. 
    */ 
    public void sendNotification(String messageBody) { 

     Intent intent = new Intent(this,Main2Activity.class); 
     intent.putExtra("messages","messages"); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
     intent.putExtra("fcm_notification", "Y"); 

     PendingIntent pendingIntent = PendingIntent.getActivity(this,0, intent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 




     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setContentTitle("Propreka") 
       .setSmallIcon(R.mipmap.new_logo) 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(Uri.parse("content://settings/system/notification_sound")) 
       .setVibrate(new long []{100,2000,500,2000}) 
       .setContentIntent(pendingIntent); 


     NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); 
     inboxStyle.setBigContentTitle(getResources().getString(R.string.app_name)); 
     Integer msg_count = db.message_count(); 
     Integer chat_count = db.chat_count(); 
     inboxStyle.setSummaryText(" "+msg_count+" messages from "+chat_count+" chat"); 

     ArrayList<ArrayList> Newchat = db.getNotifications(); 

     for (ArrayList s : Newchat) { 

      inboxStyle.addLine(s.get(0).toString()); 
     } 

     notificationBuilder.setStyle(inboxStyle); 
     NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(0, notificationBuilder.build()); 

     /*Intent resultIntent = new Intent(getBaseContext(), Main2Activity.class); 
     resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
     PendingIntent piResult = PendingIntent.getActivity(this, 1, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

     NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.new_logo) 
       .setContentTitle(getResources().getString(R.string.app_name)) 
       .setContentText(messageBody) 
       .setVibrate(new long []{0,100,10,100}) 
       .setContentIntent(piResult); 
     NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); 
     // String[] events = new String[6]; 

     inboxStyle.setBigContentTitle(getResources().getString(R.string.app_name)); 

     ArrayList<ArrayList> Newchat = db.getNotifications(); 

     for (ArrayList s : Newchat) { 

      inboxStyle.addLine(s.get(0).toString()); 
     } 

     mBuilder.setStyle(inboxStyle); 
     nManager.notify(getResources().getString(R.string.app_name),0 ,mBuilder.build());*/ 


    } 
    public void createpushnotification() 
    { 
     Log.i("Start", "notification"); 

    /* Invoking the default notification service */ 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); 

     mBuilder.setContentTitle("New Message"); 
     mBuilder.setContentText("You've received new message."); 
     mBuilder.setTicker("New Message Alert!"); 
     mBuilder.setSmallIcon(R.mipmap.new_logo); 

    /* Increase notification number every time a new notification arrives */ 
     mBuilder.setNumber(++numMessages); 

    /* Add Big View Specific Configuration */ 
     NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); 

     String[] events = new String[6]; 
     events[0] = new String("This is first line...."); 
     events[1] = new String("This is second line..."); 
     events[2] = new String("This is third line..."); 
     events[3] = new String("This is 4th line..."); 
     events[4] = new String("This is 5th line..."); 
     events[5] = new String("This is 6th line..."); 

     // Sets a title for the Inbox style big view 
     inboxStyle.setBigContentTitle("Big Title Details:"); 

     // Moves events into the big view 
     for (int i=0; i < events.length; i++) { 
      inboxStyle.addLine(events[i]); 
     } 

     mBuilder.setStyle(inboxStyle); 

    /* Creates an explicit intent for an Activity in your app */ 
     Intent resultIntent = new Intent(this, Main2Activity.class); 

     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
     stackBuilder.addParentStack(Main2Activity.class); 

    /* Adds the Intent that starts the Activity to the top of the stack */ 
     stackBuilder.addNextIntent(resultIntent); 
     PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT); 

     mBuilder.setContentIntent(resultPendingIntent); 
     NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    /* notificationID allows you to update the notification later on. */ 
     mNotificationManager.notify(notificationID, mBuilder.build()); 
    } 
} 

を示すとき、私は、受信トレイのスタイルを持って以下のコードを追加しますか?

答えて

0

データペイロードを使用して通知データを送信し、このクラスを使用して電話機を表示します。例えば

:アプリがバックグラウンドで動作しているが、FirebaseMessagingServiceからonMessageReceivedメソッドが呼び出されるときに、このデータはfirebaseにより電話にデフォルトの通知が表示されます

{ 
    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", 
    "notification" : { 
     "body" : "great match!", 
     "title" : "Portugal vs. Denmark", 
     "icon" : "myicon" 
    } 
    } 

通知ペイロードを持つ

アプリがフォアグラウンドにあるときあなたはonMessageReceivedメソッドが呼び出されます通知を送信し、データペイロード

毎回で

。したがって、必要に応じて通知を作成することができます。公式ドキュメントhereに出て、より詳細ヘッド用

{ 
    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", 
    "data" : { 
    "Nick" : "Mario", 
    "body" : "great match!", 
    "Room" : "PortugalVSDenmark" 
    }, 
} 

+0

ありがとうございました。@ Aawaz Gyawali – jithin

0

FCMメッセージには2種類あります。

  • 通知メッセージ。
  • データメッセージ。

FCM

データメッセージを送信し、それはあなたの呼び出し方法に来ます。

バックグラウンドでアプリケーションを実行すると、FCMはonMessageReceivedメソッドを呼び出しません。 代わりに、デフォルトの通知が表示されます。

+0

私はデータメッセージを使用していましたので、今はうまく動作しています... @Jarvis – jithin