0

通知をサーバーからアプリに送信します。通知が届いたときに通知の数を保持したいと思っています。通知の数で表示させたいテキストビューのアイコンに表示する必要があります。通知が開かれていないときの通知の数を維持するにはどうすればよいですか?

しかし、私はこのことをどこで行うべきですか?私はFCMを使用しているので、onMessageReceivedは、アプリケーションがフォアグラウンドにあるときに呼び出されます。

バックグラウンドサーバーからデータペイロードを送信していますが、通知がクリックされたときにのみインテントがキャッチされます。

ユーザーが通知をクリックせずに削除したのは何ですか?これを追跡する場所はどこですか?

は、今の私のコードは次のようである:家の活動

if (bundle != null) { 

      if (bundle.getString("title") != null) { 
       Intent i = new Intent(); 
       i.setAction("com.carryapp.CUSTOM_INTENT"); sendBroadcast(i); 

       FragmentManager fragmentManager = getFragmentManager(); 
       MainFragment fragment = new MainFragment(); 
       fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 
       fragmentManager.beginTransaction().replace(R.id.mycontainer, fragment, "MAIN_FRAGMENT").commitAllowingStateLoss(); 

       fragmentManager = getFragmentManager(); 
       NoticesFragment fragment3 = new NoticesFragment(); 
       Bundle bundle1 = new Bundle(); 
       bundle1.putBoolean("notification",true); 
       fragment3.setArguments(bundle1); 
       fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 
       fragmentManager.beginTransaction().replace(R.id.mycontainer, fragment3, "NOTICES_FRAGMENT").addToBackStack("H").commit(); 


      } 

     } else { 

      FragmentManager fragmentManager = getFragmentManager(); 
      MainFragment fragment = new MainFragment(); 
      fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 
      fragmentManager.beginTransaction().replace(R.id.mycontainer, fragment, "MAIN_FRAGMENT").commitAllowingStateLoss(); 
     } 

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMsgService"; 
    private String mOrderId, mBillId; 
    private Boolean mNotification; 
    private int notificationCount; 
    private SessionData sessionData; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     super.onMessageReceived(remoteMessage); 
     sessionData = new SessionData(getApplicationContext()); 
     sessionData.add("notificationCount",0); 

     notificationCount = sessionData.getInt("notificationCount",0); 


     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.describeContents()); 
     } 

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

     //get data from server notification. 


     sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle()); 
    } 

    //send notification 

    private void sendNotification(String messageBody, String title) { 


     mNotification = true; 

     notificationCount ++; 

     sessionData.add("notificationCount",notificationCount); 

     Intent intent = new Intent(this,HomeActivity.class); 
     if (!messageBody.equals("")) { 
      intent = new Intent(this, HomeActivity.class); 
      intent.putExtra("notification", mNotification); 
      intent.putExtra("title", title); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     } 

     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 
     long[] pattern = {500, 500, 500, 500, 500}; 
     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.login_logo_1) 
       .setContentTitle(title) 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setVibrate(pattern) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
    } 

} 

はこれで助けてください。ありがとう..

答えて

0

しかし、私はこのようなことをするべきですか? FCMを使用しているので、アプリがフォアグラウンドにあるときに onMessageReceivedが呼び出されます。

notification-messagesが送信されたためです。

onMessageReceivedを常に呼び出すには、データメッセージを送信する必要があります。

は、FCMのマニュアルを参照してください。
https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

PS:

私は、サーバーからの私のアプリに通知を送ります。私は 通知が到着したときに通知のカウントを保持したいと思います。私は のアイコンを表示したいのですが、通知の件数が で表示されます。

標準のAndroidは、アイコンのバッジをサポートしていません。 一部のランチャーに追加機能があるかもしれませんが、Android APIではありません。

PPS:Android Oがバッジを紹介しましたが、自動的に動作するため、手動で変更する必要はありません。

関連する問題