0

In Facebook android app we get notifications while app is forced closed. It works because there is the push notification implemented. But i have implemented the local notification in my app and i want my app notify if app is forced closed. so how can i implement it.

        void showNotify()
        {
            CharSequence details = "Notification raised";
    int reqCode=0;
    Intent notificationIntent = new Intent(context,
            NotifyListActivity.class);
    // notificationIntent.putExtra("clicked", "Notification Clicked");
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_SINGLE_TOP); // To open only one   activity
                                                // on launch.
    PendingIntent pIntent = PendingIntent.getActivity(context, reqCode,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager nM = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notify = new NotificationCompat.Builder(
            context);
    notify.setContentIntent(pIntent);
    notify.setSmallIcon(R.drawable.appicon);
    notify.setContentTitle("");
    notify.setContentText(details);
    notify.setAutoCancel(true);
    nM.notify(reqCode, notify.build());
    screenOn(context);
            }
Sebastian
  • 2,896
  • 23
  • 36
Amrit
  • 339
  • 1
  • 3
  • 10

2 Answers2

0

I dont think you should control if your app is force closed, IMO it is normal not to behave as expected if your app has been force closed.

Having said that These 2 things are completely different, when you receive a Push Notification you are receiving a message from Internet (or other source) and then you can respond to that message showing a local notification (in your notification bar), you will nedd a way to trigger your local notification in order to be shown (A repeating alarm?), notifications cannot be launched by themselves (or at least I do not know how to do it).

zozelfelfo
  • 3,776
  • 2
  • 21
  • 35
0

You can use this for the exception handling: Android exception handling best practice? With that you can execute code when an unhandled exception is thrown. Also you can catch them but this maybe is no the interesting thing for you.

And then inside the UncaughtExceptionHandler I would start a foreground service. This is necessary because app will close. I didn't test it, tell me if it worked. This would be the onCreate for the service:

    @Override public void onCreate() {
       final NotificationCompat.Builder builder = new Builder(this);
       builder.setSmallIcon(R.drawable.icon_resource);
       builder.setContentTitle(getString(R.string.notification_title));
       builder.setTicker(getString(R.string.notification_ticker));
       builder.setContentText(getString(R.string.notification_content_text));

       final Intent notificationIntent = new Intent(this, Activity_TO_OPEN_ON_NOTIFICATION_CLICKED.class);

       final PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, 0);

       builder.setContentIntent(pi);

       final Notification notification = builder.build();

       startForeground(1234, notification);
       }
Community
  • 1
  • 1
Shyri
  • 400
  • 1
  • 6
  • 15