繁体   English   中英

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

[英]Getting Push Notification Data in onMessageReceived but not showing Push Notification

我正在制作应用程序,我必须在其中显示推送通知。 我可以正确注册令牌并在 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());
}
}

尝试为您的 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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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