设备重新启动后,通知中的打开活动会有很大的延迟

时间:2018-08-31 13:02:37

标签: android notifications

我已经在我的应用程序中实现了通知,并且在正常情况下它可以正常运行,但是当我重新启动设备并尝试从收到的通知中打开活动时,它会极大延迟地打开应用程序-大约1分钟以上。

重启设备后,从通知中打开应用程序花费这么长时间是正常的吗?

创建通知:

public static NotificationCompat.Builder create(Context context, RemoteMessage remoteMessage, String channelId) {
    RemoteMessage.Notification notification = remoteMessage.getNotification();
    if (notification != null) {

        String title = getTitle(context, notification);
        String body = getBody(context, notification);
        body = body == null ? "" : body;
        PendingIntent clickAction = getClickAction(context, remoteMessage);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId)
                .setContentTitle(title == null ? "" : title)
                .setContentText(body)
                .setSmallIcon(R.drawable.ic_notification)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(body))
                .setColor(ContextCompat.getColor(context, R.color.colorPrimary))
                .setAutoCancel(true);

        if (clickAction != null) {
            builder.setContentIntent(clickAction);
        }
        return builder;
    }
}

private static PendingIntent getClickAction(Context context, RemoteMessage remoteMessage) {
    String clickAction = remoteMessage.getNotification() == null ?
            null :
            remoteMessage.getNotification().getClickAction();
    if (clickAction != null && clickAction.length() > 0) {
        Intent intent = new Intent(clickAction);
        if (remoteMessage.getData() != null) {
            Bundle extras = new Bundle();
            Map<String, String> data = remoteMessage.getData();

            for (String key : data.keySet()) {
                extras.putString(key, data.get(key));
            }
            intent.putExtras(extras);
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }
    return null;
}

并显示通知:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    NotificationCompat.Builder builder = DefaultNotification.create(this,remoteMessage, Constants.Notification.CHANNEL_PERIOD_ID);
        if (builder != null) {
            showNotification(EXTERNAL_NOTIFICATION_TAG, createUniqueNotificationID(), builder.build());
        }
    }    

private void showNotification(String tag, int notificationId, Notification notification) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(tag, notificationId, notification);
}

0 个答案:

没有答案