8

I want to use local notification in android for my application. If the app is not opened for 24 hours than a local notification is send. Can any one let me know how it should be done.

Zamani
  • 306
  • 2
  • 15
Mehroze Yaqoob
  • 804
  • 1
  • 9
  • 22
  • I think you should create Service and then check time, but for showing notification u must reading about it. =) – Gorets Jul 11 '12 at 13:25
  • Gorets, you are quite right, i do have to use some kind of service as the notification would be triggered when app is closed. Can you provide me some tutorial for that – Mehroze Yaqoob Jul 12 '12 at 04:10

3 Answers3

6

See: Local Notifications in Android? You should be able to schedule an Intent with alarm manager every hour.

Community
  • 1
  • 1
KrispyDonuts
  • 1,230
  • 1
  • 17
  • 36
  • 1
    Thanks for the quick response but can alarm manager be used if app is closed. How will the notification be triggered if app is closed? – Mehroze Yaqoob Jul 12 '12 at 04:07
  • Yes alarm manager can still be used even when the app is closed. However you wont be able to set an alarm manager when the app is installed, only when the app is loaded (atleast once) (See: http://stackoverflow.com/a/8492846/986105). Take a look at this to create a notification using alarm manager: http://smartandroidians.blogspot.com/2010/04/alarmmanager-and-notification-in.html – KrispyDonuts Jul 12 '12 at 13:33
2
Intent intent = new Intent(context, yourActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder b = new NotificationCompat.Builder(context);

    b.setAutoCancel(true)
     .setDefaults(Notification.DEFAULT_ALL)
     .setWhen(System.currentTimeMillis())         
     .setSmallIcon(R.drawable.ic_launcher)
     .setTicker("notification")            
     .setContentTitle("notification")
     .setContentText("notification")
     .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
     .setContentIntent(pIntent)
     .setContentInfo("Info");


    NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, b.build());
Makvin
  • 2,719
  • 20
  • 22
1

If you want to fire local notification with big data i.e., with multiline text in single notification with title, Ticker, icon, sound.. use following code.. I think it will help you..

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



        notificationIntent.putExtra("clicked", "Notification Clicked");
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP); // To open only one activity


            // Invoking the default notification service 

            NotificationManager mNotificationManager;
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                    context);
            Uri uri = RingtoneManager
                    .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            mBuilder.setContentTitle("Reminder");
            mBuilder.setContentText("You have new Reminders.");
            mBuilder.setTicker("New Reminder Alert!");
            mBuilder.setSmallIcon(R.drawable.clock);
            mBuilder.setSound(uri);
            mBuilder.setAutoCancel(true);

            // Add Big View Specific Configuration 
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
            String[] events = null;

                events[0] = new String("Your first line text ");
                events[1] = new String(" Your second line text");



            // Sets a title for the Inbox style big view
            inboxStyle.setBigContentTitle("You have Reminders:");

            // Moves events into the big view
            for (int i = 0; i < events.length; i++) {
                inboxStyle.addLine(events[i]);
            }

            mBuilder.setStyle(inboxStyle);

            // Creates an explicit intent for an Activity in your app 
            Intent resultIntent = new Intent(context,
                    ReminderListActivity.class);

            TaskStackBuilder stackBuilder = TaskStackBuilder
                    .create(context);
            stackBuilder.addParentStack(ReminderListActivity.class);


            // Adds the Intent that starts the Activity to the top of the stack


            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent = stackBuilder
                    .getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);

            mBuilder.setContentIntent(resultPendingIntent);
            mNotificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);


            // notificationID allows you to update the notification later  on.


            mNotificationManager.notify(999, mBuilder.build());
Pratibha Sarode
  • 1,583
  • 14
  • 16