user1756209
user1756209

Reputation: 575

Android Alarmmanager and Notification

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:

Screenshot

Upvotes: 2

Views: 694

Answers (2)

user1756209
user1756209

Reputation: 575

Replace getSystemService with context.getSystemService!

Upvotes: 2

jsmith
jsmith

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

Related Questions