通知未打开Activity onCLick [英] Notification Not open Activity onCLick

查看:69
本文介绍了通知未打开Activity onCLick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个通知,单击该通知即可打开活动.但是,当我单击通知活动时,无法打开. 这是我的代码:

I want to create a notification which should open activity when click on it. But when I click on notification activity not open . Here is my code:

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

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

您可以根据需要更改addFlagsputExtra.

You can Change addFlags or putExtra if you want.

如果使用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);

这篇关于通知未打开Activity onCLick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆