BroadcastReceiver Android Notification

I have a class that extends BroadcastReceiver, which is called whenever new Wifi check results are available (the receiver is registered in the manifest with Scan_Results passed as an intent filter).

From this class I want to be able to show a notification to the user. I am currently passing a context that is retrieved as a parameter in the onReceive method of my translation intent class for the "show notification" method of another class.

When it hits the line:

myNotificationManager.notify(notificationId, notification); 

The following exception fails:

 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 that the context that I get from the onReceive parameter is not ... due to the lack of a better phrase "right to work" ...

Any ideas? Thanks Max.

+6
source share
6 answers

ContentView is the view that is required when a notification is clicked. The code below works fine and the setLatestEventInfo() method is required.

 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); 
+14
source

Not sure why it wasn’t working before, but here is the code I worked with:

Declare the following outside of any method:

 int YOURAPP_NOTIFICATION_ID = 1234567890; NotificationManager mNotificationManager; 

Then, in the onReceive method, call the following:

 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); } 
+2
source

You need to call Notification.setLatestEventInfo ().

+2
source

Use this code with 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); 
+1
source

From what I can say, when you create a notification to go to the Notification Manager, you do not show it to view the contents. Look at the line where you actually create the notification to see if you really provide the notification for display.

0
source

For those using NotificationCompat, the following code will work:

 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
source

Source: https://habr.com/ru/post/899926/


All Articles