Reputation: 575
The following code does not work. Also try this instead of getapplicationcontext. I need a hint:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder nb = new NotificationCompat.Builder();
nb.setContentTitle("title");
nb.setContentText("message");
nb.setSmallIcon(R.drawable.ic_launcher);
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
final Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
final PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
nb.setContentIntent(contentIntent);
Notification notification = nb.getNotification();
nm.notify(0, notification);
}
}
And how could I remove the Notification after onclick?
UPDATE: I'v solved a mistake. But there is still one mistake:
Upvotes: 2
Views: 694
Reputation: 4897
BroadcastReceivers do not have a Context, so you can't do that.
You should forward the intent you received to a Service or Activity and have that class can handle the notification.
Upvotes: 0