从广播接收器显示Android通知

7

我有一个类,它继承了BroadcastReceiver。每当有新的Wifi扫描结果可用时,就会调用这个接收器(通过将接收器在清单中注册为Scan_Results广播的意图过滤器)。

从这个类中,我希望能够向用户显示通知。目前,我将接收到的上下文作为参数传递给另一个类的“显示通知”方法,该方法位于广告意图类的onReceive方法中。

当到达以下行时:

myNotificationManager.notify(notificationId, notification);

出现以下异常,操作失败:

java.lang.IllegalArgumentException: contentView required: pkg=com.mumfordmedia.trackify id=2131034122 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x0)

你有什么想法吗?我能想到的唯一原因就是,从onReceive参数中获取的上下文不太适合当前任务。

有什么建议吗? 谢谢, Max。


1
也许这个链接可以帮到你:http://stackoverflow.com/questions/2826786/pendingintents-in-notifications。无论如何,请展示更多的代码,这样我们才能提供帮助。 - Jong
2
请展示更多的代码,这样我们可以更好地了解情况。 - Kurtis Nusbaum
顺便说一句,欢迎来到 Stackoverflow!如果某个回答对您有帮助,请点赞。如果该回答成功回答了您的问题,请点击旁边的绿色勾号接受该答案。 - Kurtis Nusbaum
6个回答

13

当通知被点击时所需的视图是ContentView。以下代码可正常工作,setLatestEventInfo() 是必须的方法。

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher,
            "Hello from service", System.currentTimeMillis());
    Intent intent = new Intent(this, MainActivity.class);
    notification.setLatestEventInfo(this, "contentTitle", "contentText",
            PendingIntent.getActivity(this, 1, intent, 0));
    manager.notify(111, notification);

2
+1:使用setLatestEventInfo()方法可以解决这个错误。让人有点烦恼的是,需要写那么多行代码才能显示一个简单的通知... - ArtOfWarfare

2

之前不确定为什么不起作用,但是这是我让它正常工作的代码:

在任何方法之外声明以下内容:

int YOURAPP_NOTIFICATION_ID = 1234567890;
NotificationManager mNotificationManager;

然后在onReceive方法中调用以下内容:
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
showNotification(context, R.drawable.icon, "short", false);

接下来声明以下方法:

private void showNotification(Context context, int statusBarIconID, String string, boolean showIconOnly) {
        // This is who should be launched if the user selects our notification.
        Intent contentIntent = new Intent();

        // choose the ticker text
        String tickerText = "ticket text";

        Notification n = new Notification(R.drawable.icon, "ticker text", System.currentTimeMillis());

        PendingIntent appIntent = PendingIntent.getActivity(context, 0, contentIntent, 0);

        n.setLatestEventInfo(context, "1", "2", appIntent);

        mNotificationManager.notify(YOURAPP_NOTIFICATION_ID, n);
    }

2
你需要调用Notification.setLatestEventInfo()方法。

1

使用此代码与您的通知一起使用

Intent intent = new Intent(this, MusicDroid.class);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "This is the title", 
    "This is the text", activity);
notification.number += 1;

nm.notify(NOTIFY_ID, notification);

0

对于那些使用NotificationCompat的人,以下代码将起作用:

NotificationCompat.Builder n = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.icon).setContentText("Notify Title").setContentText("Sample Text");
    NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    Intent i = new Intent(this,MainActivity.class);
    PendingIntent ac = PendingIntent.getActivity(this, 0, i, 0);
    n.setContentIntent(ac);
    nm.notify(12222, n.build());

0
据我所知,当您创建通知以传递给通知管理器时,您没有提供内容视图来显示。请检查您实际创建通知的那一行,看看是否确实为通知提供了要显示的视图。

我从应用程序的其他位置复制并粘贴了通知脚本,那里它可以工作。因为它是从扩展BroadcastReceiver 的类中激活的,而这个类没有布局,所以没有可用的内容视图。它为什么需要一个内容视图?几个小时后我会查看文档...顺便说一句,感谢回复。 - Max Mumford

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