Cilenco Cilenco - 2 months ago 11x
Android Question

Create Notification with BroadcastReceiver

I tried to create a Notification with this Code:

private void setNotificationAlarm(Context context)
Intent intent = new Intent(getApplicationContext() , MyNotification.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 , pendingIntent);
Log.d("ME", "Alarm started");

public class MyNotification extends BroadcastReceiver
public void onReceive(Context context, Intent intent)
Log.d("ME", "Notification started");

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setContentTitle("My notification")
.setContentText("Hello World!");

NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

And here my Mainfest declaration:

android:exported="false" >

My problem now is, that the alarm is generated but the Notification isn't displayed. The BroadcastReceiver is declared in the mainfest file and there are no compiler or runtime errors.

My second problem is that
new Notification
Contructor are deprecated. What can I use instead of it?


I think you need to use

PendingIntent.getBroadcast (Context context, int requestCode, Intent intent, int flags)

instead of getService