0

I am trying to make a simple function that schedules local notifications in Android but it doesn't work. Am I missing something? i. e. a permission in the manifest or something like that? I am pretty newbie to android programming.

static int notificationId = 0;

public void scheduleLocalNotification(String text, int seconds)
{
    Context context = getApplicationContext();
    long when = System.currentTimeMillis() + seconds * 1000;
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(context, MyApp.class);

    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
    Notification notification = new Notification();
    notification.setLatestEventInfo(context, "My application", text, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.when = when;
    notificationManager.notify(notificationId, notification);
    notificationId ++;

}
Abhishek Sabbarwal
  • 3,758
  • 1
  • 26
  • 41
Chema
  • 1
  • Define "doesn't work". Does it just do nothing or do you get some sort of error? If you get an error message then please post logcat so we can help you easier – codeMagic Dec 14 '12 at 19:06
  • `PendingIntent.getActivity(context, 0, notificationIntent, 0);` You are using 0 for the `flags` parameter which isn't a valid value. Use one of the `PendingIntent` `FLAG_` constants. – Squonk Dec 14 '12 at 19:19
  • @codeMagic Is a silent error. Everything seems to be fine but the notification doesn't fire. Just that. – Chema Dec 17 '12 at 11:33
  • @Squonk It doesn't fix the problem :( – Chema Dec 17 '12 at 11:38
  • How do you call this function? Have you verified that it is called? – codeMagic Dec 17 '12 at 15:27
  • I found the problem. The notification requires an icon to show itself. Thanks you all. – Chema Dec 17 '12 at 17:40

2 Answers2

1

I think you are forget entry of new activity in android manifest file so just entry the activity like given below

 <activity
        android:name=".your activity name"
         />

and run programme

0

I found it. The problem is that I am not passing an icon resource to the notification constructor.

notification = new Notification(R.drawable.icon, text, when);
Chema
  • 1