应用程序关闭时触发通知时,通知的图像会更改并且不会更新

时间:2016-11-30 13:25:25

标签: android android-notifications notificationmanager

我正在尝试仅在应用关闭时显示通知,我通过以下代码设法实现了

public class AppMain extends Application {

public static boolean isInForeGround;
    @Override
    public void onCreate() {
        super.onCreate();
        isInForeGround = true;
    }

    @Override
    public void onTrimMemory(int level) {
        if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
            isInForeGround = false;
        }
    }
}

然后,在我收到通知消息的地方,我执行以下操作;

     @Override
            public void onMessageReceived(RemoteMessage remoteMessage) {
                if (remoteMessage.getNotification() != null) {
                    Logging.log("body" + remoteMessage.getNotification().getBody());
                    Logging.log("Title" + remoteMessage.getNotification().getTitle());

                    if (isInForeGround) {
                        Logging.log("The app is in the foreground");
                        return;
                    } else {
                        if (remoteMessage.getNotification().getTitle().equalsIgnoreCase("New Chat Added")) {
                            sendNotification(remoteMessage.getNotification(), ChatActivity.class);
                        } else if (remoteMessage.getNotification().getTitle().equalsIgnoreCase("New Announcement Added")) {
                            sendNotification(remoteMessage.getNotification(), AnnouncementActivity.class);
                        } else {
                            sendNotification(remoteMessage.getNotification(), NotificationsActivity.class);
                        }
                    }

                } else {
                    Logging.log(" Notification is null ");
                }
            }

创建通知的功能是;

private void sendNotification(RemoteMessage.Notification messageBody, Class activityClass) {

    Intent intent = new Intent(this, activityClass);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(messageBody.getTitle())
            .setContentText(messageBody.getBody())
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    notificationBuilder.setSmallIcon(getNotificationIcon());


    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());

}
 private int getNotificationIcon() {
        boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
        return useWhiteIcon ? R.mipmap.notification_ : R.mipmap.notification_;
    }

问题是关闭应用时,通知图标会更改为应用启动器,并且会多次创建,FLAG_UPDATE_CURRENT和FLAG_CANCEL_Current从不工作

在应用程序处于前台时测试了解决方案,并且使用wright图标工作正常并更新了之前的通知,我需要知道问题的原因和可能的解决方案。

0 个答案:

没有答案