0

Can i open app from notification showing app is running in background. I tried following code

String NOTIFICATION_CHANNEL_ID = "xxx.xxxx.xxxx.myapplication";
        String NOTIFICATION_CHANNEL_NAME = "hhyydddddd";
        String NOTIFICATION_CHANNEL_DESC = "DDDDDDDDDDD";
        Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,notificationIntent,0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setContentTitle("App is running in background")
                .setContentText("HELLO")
                .setTicker("TICKER").setFullScreenIntent(pendingIntent,true)
                .setContentIntent(pendingIntent);
        Notification notification = builder.build();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        if(Build.VERSION.SDK_INT>=26) {
            NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription(NOTIFICATION_CHANNEL_DESC);
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(channel);

        }
        startForeground(NOTIFICATION_ID, notification);

Using this code i can open app in devices having android pie, But when i try same code on device having oreo, click on notification leads to app info page.

Vineeth Mohan
  • 2,584
  • 4
  • 17
  • 31

1 Answers1

0

Do you use BroadcastReceiver class? When you close the app, broadcast can listener this action. So, if you create a BroadcastReceiver class, you don't take this problem.

public class ClosedBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(new Intent(context, HomePage.class));
        } else {
            context.startService(new Intent(context, HomePage.class));
        }
    }
}
AlpYuktug
  • 178
  • 1
  • 2
  • 13