从通知发送的 PendingIntent

发布于 2024-09-05 10:37:00 字数 1645 浏览 1 评论 0原文

我想要完成的是通过通知管理器发送通知 一旦单击,仅当应用程序当前正在运行时才会在应用程序中执行某些操作。 我尝试使用:

notification.contentIntent = PendingIntent.getActivity(this, nNotificationCounter, Someintent, PendingIntent.FLAG_NO_CREATE)

一旦尝试使用通知,总会导致异常。 我切换到:

Notification notification = new Notification(icon, tickerText, when);

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.some_notification);
contentView.setTextViewText(R.id.title, sTitle);
contentView.setTextViewText(R.id.text, sText);
notification.contentView = contentView;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.number = nNotificationCounter;

Intent notificationIntent = new Intent(this, MainWindow.class).setAction(ACTION_RESET_MESSGE_COUNTER);
notification.contentIntent = PendingIntent.getBroadcast(this, nNotificationCounter, notificationIntent, 0);

尽管这段代码不会导致异常。它不会调用我的 BroadcastReceiver,其定义如下:

public class IncomingReceiver extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(ACTION_RESET_MESSGE_COUNTER)) {
                    System.out.println("GOT THE INTENT");
                    return;
                }

            }
        }

并在 onCreate 中设置:

IntentFilter filter = new IntentFilter(ACTION_RESET_MESSGE_COUNTER);
        IncomingReceiver receiver = new IncomingReceiver();
        context.registerReceiver(receiver, filter);

有人看到代码有问题吗? 或者,当单击通知时,我将如何获取消息,但如果尚未创建,则不创建任何活动。

编辑:添加了意图创建和通知创建。

谢谢, 汤姆

What im trying to accomplish is to send a notification through the notification manager
that once clicked will do something in the application only if its currently running.
i have tried to use:

notification.contentIntent = PendingIntent.getActivity(this, nNotificationCounter, Someintent, PendingIntent.FLAG_NO_CREATE)

Which allways caused an exception once trying to use the notify.
I switched to:

Notification notification = new Notification(icon, tickerText, when);

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.some_notification);
contentView.setTextViewText(R.id.title, sTitle);
contentView.setTextViewText(R.id.text, sText);
notification.contentView = contentView;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.number = nNotificationCounter;

Intent notificationIntent = new Intent(this, MainWindow.class).setAction(ACTION_RESET_MESSGE_COUNTER);
notification.contentIntent = PendingIntent.getBroadcast(this, nNotificationCounter, notificationIntent, 0);

and although this code doesn't cause an exception. it doesnt call my BroadcastReceiver which is defined as follows:

public class IncomingReceiver extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(ACTION_RESET_MESSGE_COUNTER)) {
                    System.out.println("GOT THE INTENT");
                    return;
                }

            }
        }

and set in the onCreate:

IntentFilter filter = new IntentFilter(ACTION_RESET_MESSGE_COUNTER);
        IncomingReceiver receiver = new IncomingReceiver();
        context.registerReceiver(receiver, filter);

Does anyone see something wrong with the code?
Or how would i go about to get messages when the notification is clicked, but not create any activity if it isn't already created.

edit: added the intent creation and notification creation.

Thanks,
Tom

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋心╮凉 2024-09-12 10:37:00

您很可能无法收到广播,因为您错误地创建了 IntentIntent(Context, Class) 告诉系统创建一个意图来“创建”该类。换句话说,该意图仅直接针对硬编码类。只需使用 new Intent(String) 或在您的具体情况下 new Intent(ACTION_RESET_MESSGE_COUNTER),您将创建我所说的“广播”意图或文档所说的“行动”意图。这样,意图将被定向到正在收听该特定广播/操作的任何人。

You're most likely not getting the broadcast because you are incorrectly creating your Intent. Intent(Context, Class<?>) tells the system to create an intent that will 'create' that class. In other words, that intent is directly solely to the hard coded class. Just use new Intent(String) or in your specific case new Intent(ACTION_RESET_MESSGE_COUNTER) and you'll create what I call a 'broadcast' intent or what the docs call an 'action' intent. That way the intent will be directed to anyone who is listening for that specific broadcast/action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文