在onMessageReceived中获取推送通知数据,但未显示推送通知

时间:2018-09-07 05:23:26

标签: android firebase firebase-cloud-messaging

我正在制作必须显示推送通知的应用程序。我可以正确注册令牌,也可以在FirebaseMessageService remoteMessage中获取数据。但是它没有显示通知。我要检查类型。如果type = 3,则仅显示通知。然后,我想将所有这些信息保存在本地,以便以后显示。

后端将此数据发送到fcm

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

FCM响应此数据(示例):

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类

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

1 个答案:

答案 0 :(得分:1)

尝试为您的notificationBuilder添加优先级: .setPriority(NotificationCompat.PRIORITY_DEFAULT);

也许在待定意图声明中更改标志:PendingIntent.FLAG_UPDATE_CURRENT

如果您正在使用API​​ +26进行测试,则必须创建通知通道

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