通知消息意外设置为“通知标题”

时间:2019-06-28 07:00:21

标签: android push-notification notifications android-notifications android-notification-bar

关于此问题Notification content text being changed based on different scenarios,我面临着通知到达时它将正确显示其所有消息和标题的问题,但是当我下拉系统托盘以查看通知时,通知的标题和消息将变为相同!谁能解释这种情况背后的原因? 这是我实现的代码-

@RequiresApi(api = Build.VERSION_CODES.O)
private void sendChatmessageNotification(String title, String message, String senderId){
    Log.d(TAG, "sendChatmessageNotification: building a chatmessage notification");

    NotificationChannel mChannel=null;

    //get the notification id
    int notificationId = buildNotificationId(senderId);

    // Instantiate a Builder object.
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "default_channel_id_chat");
    // Creates an Intent for the Activity
    Intent pendingIntent = new Intent(this, MainActivity.class);
    // Sets the Activity to start in a new, empty task
    pendingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    pendingIntent.putExtra("Id", senderId);
    pendingIntent.putExtra("Name", title.split(":")[1].trim());

    // Creates the PendingIntent
    PendingIntent notifyPendingIntent =
            PendingIntent.getActivity(
                    this,
                    0,
                    pendingIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


        mChannel = new NotificationChannel("default_channel_id_chat", getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH);
        // Configure the notification channel.
        mChannel.setDescription(message);
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.GREEN);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        mNotificationManager.createNotificationChannel(mChannel);

        //add properties to the builder
        builder.setSmallIcon(R.drawable.icons1)
                .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(),
                        R.drawable.icons1))
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setContentTitle(title)
                .setContentText("test  " +message)
                .setColor(getResources().getColor(R.color.blue_btn_bg_color))
                .setAutoCancel(true)
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText(title).setSummaryText(message))
                .setNumber(mNumPendingMessages)
                .setOnlyAlertOnce(true);

    builder.setContentIntent(notifyPendingIntent);


    mNotificationManager.notify(notificationId, builder.build());

}

private int buildNotificationId(String id){
    Log.d(TAG, "buildNotificationId: building a notification id.");

    int notificationId = 0;
    for(int i = 0; i < 9; i++){
        notificationId = notificationId + id.charAt(i);
    }
    Log.d(TAG, "buildNotificationId: id: " + id);
    Log.d(TAG, "buildNotificationId: notification id:" + notificationId);
    return notificationId;
}

这是图片 enter image description here

0 个答案:

没有答案