简体   繁体   中英

Getting Push Notification Data in onMessageReceived but not showing Push Notification

I am making app in which i have to show push notification. I can register token properly and also getting data in FirebaseMessageService remoteMessage. But it is not showing notification. I want to check type. if type=3 then only show notification. And after that i want to save all this info in local to display it later.

Backend sending this data to fcm

$fields = array(
'registration_ids' => $token,
'priority' => 10,
'notification' => array('title' => 'digimkey', 'body' => $message 
,'sound'=>'Default','image'=>'Notification Image' ),
);

FCM responding with this data(Sample):

Message data payload: {type=3, title=Ravi Kulkarni - Marathi, partner_id=8, student_parent_list=2894|3052|Roshni Kaushal|, message=home, tickerText=New Message from Ravi Kulkarni}

MyFirebaseMessagingService class

Override
public void onMessageReceived(RemoteMessage remoteMessage) {
   if (remoteMessage.getData().size() > 0){
       Log.d(TAG, "Message data payload: " + remoteMessage.getData());
       Map<String, String> params = remoteMessage.getData();
       JSONObject object = new JSONObject(params);
       Log.e("JSON_OBJECT", object.toString());

       try {
           String title = object.getString("title");
           String partnerId = object.getString("partner_id");
           String studentInfo = object.getString("student_parent_list");
           String message = object.getString("message");
           String studentName = studentInfo.split("|").toString().replace("|","");
           String ticker = object.getString("tickerText");

           String notStatus = SharedPreferenceManager.getmInstance(getApplicationContext()).getNotification();

           if (notStatus.equals("YES")){
               SendNotification(message,studentName,ticker);
           }
       } catch (JSONException e) {
           e.printStackTrace();
       }

   }

}

private void SendNotification(String messageBody,String studentName,String ticker){
    Intent intent = new Intent(this,Notification_All.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_id);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext(),channelId)
            .setSmallIcon(R.drawable.logo)
            .setTicker(ticker)
            .setContentTitle("DIGIMKEY")
            .setContentText(messageBody+"for"+studentName)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0,notification.build());
}
}

Try adding priority to your notificationBuilder: .setPriority(NotificationCompat.PRIORITY_DEFAULT);

Maybe changing the flag in your Pending intent declaration: PendingIntent.FLAG_UPDATE_CURRENT

If you are testing with API +26 you have to create notification channel

    private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = CHANNEL_NAME;
        String description = CHANNEL_DESCRIPTION;
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM