当前位置 : 主页 > 手机开发 > android >

android – 通知不打开Acivity onCLick

来源:互联网 收集:自由互联 发布时间:2021-06-11
我想创建一个通知,点击它时应该打开活动.但是当我点击通知活动时没有打开. 任何帮助将不胜感激.这是我的代码: NotificationManager notificationManager = (NotificationManager) context .getSystemServi
我想创建一个通知,点击它时应该打开活动.但是当我点击通知活动时没有打开.
任何帮助将不胜感激.这是我的代码:

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);
用这个:

Notification.Builder notification = new Notification.Builder(context)
.setContentIntent(getDialogPendingIntent(Text, intentName));

private PendingIntent getDialogPendingIntent(String dialogText,
            String intentname) {
        return PendingIntent.getActivity(
                context,
                dialogText.hashCode(),
                new Intent(ACTION_DIALOG)
                        .putExtra(Intent.EXTRA_TEXT, dialogText)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .setAction(intentname), 0);
}

getDialogPendingIntent(Text, intentName) : intentName=com.yourProject.exrta.yourIntentName

如果需要,您可以更改addFlags或putExtra.

如果使用Intent Name调用不起作用,请使用类似的类,它必须工作:

Intent notificationIntent = new Intent(MainActivity.this, TestActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                            Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);

NotificationManager notificationManager = (NotificationManager) MainActivity.this
                        .getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder notification = new Notification.Builder(MainActivity.this)
                .setContentTitle("Message Received")
                .setSmallIcon(R.drawable.ic_launcher)
                .setAutoCancel(true)
                .setContentIntent(intent);

Notification notificationn = notification.getNotification();
notificationManager.notify(1, notificationn);
网友评论