-1

hi i am trying to display the push notification using BroadcastReceiver it works fine when the app is minimized but when the app is closed i can not display the push notification.

and my question is what is possible techniques or solution to display the push notification when the app is closed?

bellow is code for notification using broadcast receiver

    public class Alarm extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String message=intent.getStringExtra("message");
        String title=intent.getStringExtra("title");
        String click_action=intent.getStringExtra("click_action");
      notification(context,message,title,click_action,intent);
    }

    private void notification(Context context, String message, String title, String click_action, Intent intent) {

        Toast.makeText(context, title + message + click_action, Toast.LENGTH_SHORT).show();
        if (click_action.equals("Time_LineActivity")) {
            intent = new Intent(context, Time_LineActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        } else if (click_action.equals("MainActivity")) {
            intent = new Intent(context, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        } else {
            intent = new Intent(context, MainActivity.class);
            intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
        }
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        Notification.Builder notification = new Notification.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notification.build());

    }
}

and bellow is my FirebaseMessagingService code

public class MyFirebaseMessagingService extends  FirebaseMessagingService {
    private static final String TAG = "MyFirebaseMsgService";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getData().get("title" +" "+"body" +" "+"click_action"));
        String title=remoteMessage.getData().get("title");
        String message=remoteMessage.getData().get("body");
        String click_action=remoteMessage.getData().get("click_action");
        sendNotification(title,message,click_action);
    }
    private void sendNotification(String title, String message, String click_action) {
        Intent broadcastedIntent=new Intent(this, Alarm.class);
        broadcastedIntent.putExtra("message", message);
        broadcastedIntent.putExtra("title", title);
        broadcastedIntent.putExtra("click_action", click_action);
        sendBroadcast(broadcastedIntent);
    }
}

Thanks in advance..

Vignesh
  • 355
  • 1
  • 4
  • 17
  • Possible duplication of https://stackoverflow.com/questions/24313539/push-notifications-when-app-is-closed – Kavin-K Nov 23 '17 at 03:33
  • 1
    You can use Firebase Cloud Messagin (FCM) system to display a push notification when the app is closed. Here is a [tutorial](https://www.simplifiedcoding.net/firebase-cloud-messaging-tutorial-android/) for that – Nazmul Haque Nov 23 '17 at 03:38
  • is have implemented that actually i have called this broadcast receiver from the FirebaseMessagingService only but still is not working – Vignesh Nov 23 '17 at 03:50
  • Push notifications are supposed to display even when the app is close. If its not working that way, probably it has to do something with your OS. May be you are running app on a restrictive rom like MIUI. [Read this](https://medium.freecodecamp.org/why-your-push-notifications-never-see-the-light-of-day-3fa297520793) for more. – Aswin P Ashok Nov 23 '17 at 05:04
  • ok my mobile is asus zenfone 2 laser with android 6.0.1 on its. Is it some thing with that? – Vignesh Nov 23 '17 at 09:50

1 Answers1

0

Ok thanks for all your valuable comment i know there are several threads on this topic but nothing worked for me this made me to go mad.

and now the solution:

it was dude to the Mobile manager app in my phone and it has the option to disable auto-start permission of some application and i added in the auto start list and now it works well. I have even seen this in other smart phones to like Xiaomi, Oppo, One Plus. This worked for me.

Vignesh
  • 355
  • 1
  • 4
  • 17