待定意图不起作用,通知无法打开活动。

3

你好,我有一个在我的应用程序中显示通知的函数,但是突然间我遇到了一个问题,当我点击通知时,它没有将我带到我在.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);
}

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


我改变了活动的名称,它就起作用了,我又改回来了,但是它却不起作用,所以我想这只是其中一种情况,稍后我会仔细检查我的代码,看看真正的问题是什么。 - user3718908x100
1个回答

2

这是我使用的代码,它完美地运行着。我将其放在了一个服务中,但您可以在任何活动中使用:

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());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接