相关疑难解决方法(0)

Android状态栏通知 - 选择通知时打开正确的活动

单击时,我一直遇到通知无法打开/转到正确活动的问题.

我的通知代码(位于扩展服务的类中):

Context context = getApplicationContext();

    CharSequence contentTitle = "Notification";

    CharSequence contentText = "New Notification";

    final Notification notifyDetails =
        new Notification(R.drawable.icon, "Consider yourself notified", System.currentTimeMillis());

    Intent notifyIntent = new Intent(context, MainActivity.class);

    PendingIntent intent =
          PendingIntent.getActivity(context, 0,
          notifyIntent,  PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL);

    notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);

    ((NotificationManager)getSystemService(NOTIFICATION_SERVICE)).notify(NOTIFICATION_ID, notifyDetails);
Run Code Online (Sandbox Code Playgroud)

如果在创建服务的应用程序打开时单击通知,通知将消失(由于FLAG_AUTO_CANCEL),但活动未切换.

如果我从主屏幕点击通知,通知将消失,我的应用程序将被带到前面,但是它仍然在进入主屏幕之前打开的活动,而不是进入主屏幕.

我究竟做错了什么?如何指定将被提取的活动?

notifications android android-activity

7
推荐指数
1
解决办法
1万
查看次数

Android点击通知不会打开附加的Activity

当我点击状态栏中的通知时,我想打开一个活动.我在StackOverflow上看到了这个问题,但这些答案都不适用于我.此问题仅在Lollipop设备上出现,最佳重现方式是:1.启动应用程序.2.重新启动应用程序.3.接收推送通知.4.单击通知并尝试3-4推送通知.

以下是我的代码:我也尝试了以下内容:

  1. 添加Intent.ACTION_MAIN
  2. Intent.CATEGORY_LAUNCHER的意图
  3. PendingIntent.FLAG_UPDATE_CURRENT.
  4. 我的AndroidManifest.xml中的android:exported ="true"

但没有任何作用.我只在Lollipop设备上看到这个问题.请注意我间歇地看到这个问题.这是与缓存的意图有关还是未交付的内容.请帮忙.

PendingIntent pendingIntent = getPendingIntent(context, payload);
Builder notificationBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_stat)
            .setContentTitle(res.getString(R.string.app_name))
            .setContentText(payload.getMessage())
            .setTicker(payload.getMessage())
            .setContentIntent(pendingIntent)
            .setSound(soundUri);

private PendingIntent getPendingIntent(Context context, NotificationPayload payload) {
    int requestID = (int) System.currentTimeMillis();
    Intent intent = new Intent(context, DeepLinkActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setPackage(BuildConfig.APPLICATION_ID);
    intent.putExtra(NotificationHelper.KEY_NOTIFICATION_PAYLOAD, payload);
    return PendingIntent.getActivity(context, requestID, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT);
}
Run Code Online (Sandbox Code Playgroud)

android push-notification android-activity android-pendingintent

5
推荐指数
1
解决办法
1808
查看次数

通知不公开Acivity onCLick

我想创建一个通知,点击它时应该打开活动.但是当我点击通知活动时没有打开.任何帮助将不胜感激.这是我的代码:

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

    Intent intent = new Intent(context, MessageReceivedActivity.class);
    intent.putExtra("payload", payload);
    intent.setAction(Long.toString(System.currentTimeMillis()));
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent, PendingIntent.FLAG_CANCEL_CURRENT);

    Notification.Builder notification = new Notification.Builder(context)

    .setContentTitle("Message Received")
    .setSmallIcon(R.drawable.icon)
    .setContentText(payload)
    .setContentIntent(pendingIntent)
    .setAutoCancel(true);

    Notification notificationn = notification.getNotification();

    notificationManager.notify(0, notificationn);
Run Code Online (Sandbox Code Playgroud)

android android-notifications

3
推荐指数
1
解决办法
1万
查看次数