简体   繁体   English

Android显示来自BroadcastReceiver的通知

[英]Android show notification from BroadcastReceiver

I have a class which extends BroadcastReceiver that gets called whenever new Wifi scan results are available (the receiver is registered in the manifest with the Scan_Results broadcast as the intent-filter). 我有一个扩展BroadcastReceiver的类,只要有新的Wifi扫描结果可用就会被调用(接收器在清单中注册,并将Scan_Results广播作为intent-filter)。

From this class, I want to be able to show a notification to the user. 从这个课程,我希望能够向用户显示通知。 Currently, I pass the context that is received as a parameter in the onReceive method of my broadcast intent class to a "show notification" method of another class. 目前,我将在广播意图类的onReceive方法中作为参数接收的上下文传递给另一个类的“show notification”方法。

When it gets to the line: 当它到达该行:

myNotificationManager.notify(notificationId, notification);

it fails with the following exception: 它失败,出现以下异常:

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

Any idea why this is happening? 知道为什么会这样吗? All I can think of is because the context that I am getting from the onReceive parameter is not ... for lack of a better phrase, "right for the job"... 我能想到的只是因为我从onReceive参数得到的上下文不是......因为没有更好的短语,“适合工作”......

Any ideas? 有任何想法吗? Thanks, Max. 谢谢,马克斯。

ContentView is the view which is required when the notification is clicked. ContentView是单击通知时所需的视图。 The code below works fine and setLatestEventInfo() is required method. 下面的代码工作正常, 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);

Not sure exactly why it wasnt working before but here is the code I got it working with: 不知道为什么它之前没有工作,但这里是我得到它的代码:

Declare the following outside of any method: 在任何方法之外声明以下内容:

int YOURAPP_NOTIFICATION_ID = 1234567890;
NotificationManager mNotificationManager;

Then in the onReceive method call the following: 然后在onReceive方法中调用以下内容:

mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
showNotification(context, R.drawable.icon, "short", false);

Then declare the following method: 然后声明以下方法:

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

您必须调用Notification.setLatestEventInfo()。

Use this code along with your notification 将此代码与您的通知一起使用

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

For those who are using NotificationCompat, the following code will work: 对于使用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());

From what I can tell, when you're creating you notification to pass to the Notification manager, you're not giving it a content view to display. 据我所知,当您创建通知以传递给通知管理器时,您不会给它显示内容视图。 Review the line where you actually create the notification to see if you're actually giving the notification a view to display. 查看实际创建通知的行,以查看您是否实际向通知提供了要显示的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM