Android推送通知:应用程序关闭或终止时样式不起作用

时间:2016-09-13 08:49:26

标签: android android-notifications firebase-cloud-messaging firebase-notifications

    NotificationCompat.InboxStyle notifInbox = new NotificationCompat.InboxStyle();
notifInbox.addLine("First Word");
notifInbox.addLine("Second Word");
notifInbox.addLine("Third Word");

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)

                .setSmallIcon(R.drawable.notificon)
                .setContentTitle(title)
                .setSubText("FROM: SYSTEM")
                .setContentInfo("X")            
                .setStyle(notifInbox)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)               
                .setContentIntent(pendingIntent);

为什么我的申请被关闭或被杀,然后我发送通知,风格不起作用?或者如何防止通知在应用程序关闭或终止时显示?我正在使用FCM。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

它为我工作你可以试试。

 if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
     // Do something for lollipop and above versions
     NotificationCompat.Builder mBuilder =
     new NotificationCompat.Builder(this)
             .setSmallIcon(R.drawable.notification_icon)
             .setPriority(Notification.PRIORITY_HIGH)
             .setContentTitle(TextUtils.isEmpty(title) ? "XYZ" : title)
             .setStyle(new NotificationCompat.BigTextStyle()
             .bigText(msg))
             .setContentText(msg)
             .setColor(getResources().getColor(R.color.colorPrimary));

    mBuilder.setSound(Uri.parse("content://settings/system/notification_sound"));
    mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
    mBuilder.setAutoCancel(true);
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
} else {
    // do something for phones running an SDK before lollipop
    NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle(TextUtils.isEmpty(title) ? "xyz" : title)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(msg))
                        .setContentText(msg);

    mBuilder.setSound(Uri.parse("content://settings/system/notification_sound"));
    mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
    mBuilder.setAutoCancel(true);
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}