【问题标题】:Pending Intent Not Working, Notification Does Not Open Activity待定意图不起作用,通知未打开活动
【发布时间】:2017-05-29 21:45:37
【问题描述】:

您好,我有以下在我的应用程序中显示通知的功能,但是我突然开始遇到一个问题,当我单击通知时,它不会将我带到我在 .setContentIntent() 中指定的活动,这是我的代码:

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void getNotifications() {
    ArrayList<com.xxxxxx.app234929.models.Notification> notifications = mNotificationsTable.get();

    int requestId = (int) System.currentTimeMillis();
    Intent notificationsIntent = new Intent(getApplicationContext(), NotificationsActivity.class);
    notificationsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), requestId,
            notificationsIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.InboxStyle inbox = new NotificationCompat.InboxStyle(new NotificationCompat.Builder(getApplicationContext())
            .setSmallIcon(R.drawable.ic_stat_mtgh_notification)
            .setColor(getResources().getColor(R.color.colorPrimary))
            .setContentTitle("xxxxxx")
            .setContentText("You have "+(notifications.size() > 1 ? "new updates" :"a new update"))
            .setNumber(notifications.size())
            .setContentIntent(intent)
            .setDefaults(Notification.DEFAULT_VIBRATE |
                    Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS))
            .setBigContentTitle("xxxxxx");

    for (int i=0; i < notifications.size(); i++) {
        inbox.addLine(notifications.get(i).getMessage());
    };

    Notification notification = inbox.build();

    NotificationManager notificationManager = (NotificationManager) getApplicationContext()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);
}

我搜索了很多解决方案并尝试了它们,但没有一个对我有用,所以现在我不知道我在这里做错了什么。

【问题讨论】:

  • 我更改了他的活动名称并且它起作用了,将它改回来但它不起作用所以我会说这只是其中之一,稍后将通过我的代码查看真正的问题是什么。

标签: android android-notifications


【解决方案1】:

这是我使用的代码并且运行良好,我将它用于服务,但您可以在任何活动中使用:

Intent myIntent = new Intent(MyActivity.this, ActivityIWantToOpenOnClick.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MyActivity.this, 0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder ncomp = new NotificationCompat.Builder(MyActivity.this);
ncomp.setContentTitle("Message Tittle");
ncomp.setContentText("Message Text");
ncomp.setTicker("Message Ticker");                                  
ncomp.setSmallIcon(R.drawable.your_icon);
ncomp.setAutoCancel(true);
ncomp.setContentIntent(pendingIntent);
nManager.notify((int) System.currentTimeMillis(), ncomp.build());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-04
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多