CallMyName
CallMyName

Reputation: 84

Ensure the notification remain even when the application is closed

I am working on an application that will show a notification when the alarm is triggered. The notification will show only when the application is still running however I wish the notification will stay even when the application is closed so when the user select the notification it will run the application again. Is there any way on doing it? Any help will be greatly appreciated. I will also appreciated any examples or tutorials provided. Thank you very much.

Upvotes: 0

Views: 984

Answers (2)

Shial
Shial

Reputation: 1406

How about that I'm using

alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

and I want to push notification in future by alarmMenager It works while app is still running, while I call allarm menager to set notify in futer and close app my notification didn't show. WHat i have to do ?

here u got my event sender:

Calendar cal = Calendar.getInstance();
 cal.set(Calendar.HOUR, HOUR_OF_DAY);
 cal.set(Calendar.MINUTE, MINUTE);
 //cal.add(Calendar.SECOND, SECOND_OF_DAY);
 Intent intent = new Intent(UnityPlayer.currentActivity, TimeAlarm.class);
 intent.putExtra("alarm_status", statusMessage);
 intent.putExtra("alarm_title", title);
 intent.putExtra("alarm_content", content);
 Log.i("SenderEvent ", "przygotowane dane");
 PendingIntent sender = PendingIntent.getBroadcast(UnityPlayer.currentActivity.getApplicationContext(), REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
 am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

And Reciever:

Bundle bundle = intent.getExtras();
        String statusMessage = bundle.getString("alarm_status");
        String title = bundle.getString("alarm_title");
        String content = bundle.getString("alarm_content");
        nm = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(), 0);
Notification notif =new Notification();//R.drawable.ic_launcher,statusMessage, System.currentTimeMillis());;
        //notif.largeIcon = bitmap;
        notif.icon =2130837504;
        notif.tickerText=statusMessage;
        notif.when= System.currentTimeMillis(); 
        /*
        new Notification(0,
                statusMessage, System.currentTimeMillis());*/
        notif.setLatestEventInfo(context, title, content, contentIntent);
        nm.notify(NOTIFY_ME_ID, notif);

Whats wrong with that to push notification in the future while app is close ?

Upvotes: 3

Andres Gallo
Andres Gallo

Reputation: 681

First you need to create your notification using a service. I had this same question before. I was using phonegap, and creating the notifications from a plugin, but it turns out only services can run in the background. In the service I then add my code to create the notification, and for the intent I use a broadcast receiver.

      notificationManager = (NotificationManager) context.getSystemService(android.content.ContextWrapper.NOTIFICATION_SERVICE);
      note = new Notification(imageResource, tickerText, System.currentTimeMillis() );

     // Intent notificationIntent = new Intent(context, path.to.class);
      Intent notificationIntent = new Intent(context, package.path.to.receiver.class);

      notificationIntent.setAction(Intent.ACTION_VIEW);
      notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

      //contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
      contentIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      note.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

As you can see in the code above I commented out getActivity, and changed it to getBroadcast. This being run in a service means you can get notifications while app is closed. For the intent in the receiver to be able to open your app if closed add

...

@Override
public final void onReceive(Context context, Intent intent) {
    Log.v('TAG','TEST');


     //start activity
    Intent i = new Intent();
    i.setClassName("package.path", "package.path.mainActivity");
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
}
...

And xml

 <receiver android:name="path.to.receiver" android:label="@string/app_name">
   <intent-filter>
      <action android:name="path.to.receiver.BROADCAST" />
   </intent-filter>
</receiver>

I hope this helps

Upvotes: 0

Related Questions