Ad

Trying To Open A Fragment Upon Push Notification Click. Not Working

- 1 answer

I am trying to open a fragment via a push notification that is triggered in firebase. It is not working. Here is the MessageService.java

private void showNotification(String title, String body) {
        String NOTIFICATION_CHANNEL_ID = "plan.services.test";

        Intent openMain = new Intent(this, MainActivity.class);
        openMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        openMain.putExtra("checkNotification", "openOrder");
        Log.d(TAG, "order string in msg service:"+openMain.getStringExtra("checkNotification"));
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent orderIntent = PendingIntent.getActivity(OrderMsgService.this, 1, openMain, PendingIntent.FLAG_UPDATE_CURRENT);
        Log.d(TAG, "order intent:"+ orderIntent);

        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
            currentapiVersion = R.drawable.icon_notification;
        } else{
            currentapiVersion = R.mipmap.image_main_logo;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationCompat.Builder notificationChannelBuilder = new NotificationCompat.Builder(OrderMsgService.this, NOTIFICATION_CHANNEL_ID) // For Newer API
                    .setSmallIcon(currentapiVersion)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setAutoCancel(true)
                    .setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
                    .setWhen(System.currentTimeMillis())
                    .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                    .setContentIntent(orderIntent);

            NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_HIGH);

            channel.enableLights(true);
            channel.setLightColor(Color.GREEN);
            channel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            channel.enableVibration(true);
            channel.setDescription("Plan Notifications");
            notificationManager.createNotificationChannel(channel);

            notificationManager.notify(1, notificationChannelBuilder.build());
        } else {
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)//for older API

                    .setSmallIcon(currentapiVersion)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setAutoCancel(true)
                    .setPriority(Notification.PRIORITY_HIGH)
                    .setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
                    .setWhen(System.currentTimeMillis())
                    .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                    .setContentIntent(orderIntent);

            notificationManager.notify(1, notificationBuilder.build());
        }
    }

And here is the onCreate method in MainActivity

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String orderFragment = getIntent().getStringExtra("openOrder");
        Log.d(TAG, "order string in main activity:"+orderFragment);
        // Launch order if triggered from notification; otherwise open mealplan
        if (orderFragment != null) {
            if (orderFragment.equals("openOrder")) {
                getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new OrderFragment(), null).commit();
            }
        } else {
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new MealplanFragment(), null).commit();
        }
    }

I dont care if I send a boolean, int or string. I just want to send some sort of a flag so that I can open the necessary fragment.

UPDATE Here is the working code. I still dont know what I did or why it works. But I am not touching this again and from now on will resort to just copy+pasting this.

private void showNotification(String title, String body) {

        long notificationId = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        Intent openMain = new Intent(this, MainActivity.class);
        openMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        openMain.putExtra("checkNotification", "openOrder");

        PendingIntent orderIntent = PendingIntent.getActivity(this, (int) (Math.random() * 100), openMain, PendingIntent.FLAG_UPDATE_CURRENT);
        Log.d(TAG, "order intent:"+ orderIntent);

        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
            currentapiVersion = R.drawable.icon_notification;
        } else{
            currentapiVersion = R.mipmap.image_main_logo;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationCompat.Builder notificationChannelBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID) // For Newer API

                    .setSmallIcon(currentapiVersion)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setAutoCancel(true)
                    .setWhen(System.currentTimeMillis())
                    .setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
                    .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                    .setContentIntent(orderIntent);

            NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_HIGH);

            channel.enableLights(true);
            channel.setLightColor(Color.BLUE);
            channel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            channel.enableVibration(true);
            channel.setDescription("Plan Notification");
            notificationManager.createNotificationChannel(channel);
            notificationManager.notify((int) notificationId, notificationChannelBuilder.build());

        } else {
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)//for older API

                    .setSmallIcon(currentapiVersion)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setAutoCancel(true)
                    .setPriority(Notification.PRIORITY_HIGH)
                    .setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
                    .setWhen(System.currentTimeMillis())
                    .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                    .setContentIntent(orderIntent);

            notificationManager.notify((int) notificationId, notificationBuilder.build());
        }
    }
Ad

Answer

I was In the Same Problem in the new Api (28 ). For newer Api there are two parameters in NotificationCompat.Builder. So, I used Notification this way and works like a charm :

 private static final String CHANNEL_ID = "nazim_borac_driver_channelId";

private void showNotificationSystemTray(String msg, String pickupLocation, String dropOffLocation) {


        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        long notificatioId = System.currentTimeMillis();

Intent openMain = new Intent(this, MainActivity.class);
        openMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        openMain.putExtra("checkNotification", "openOrder");

        PendingIntent contentIntent = PendingIntent.getActivity(this, (int) (Math.random() * 100), openMain, PendingIntent.FLAG_UPDATE_CURRENT);

        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
            currentapiVersion = R.mipmap.ic_pickup;
        } else{
            currentapiVersion = R.mipmap.ic_launcher;
        }


      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {


            NotificationCompat.Builder notificationChannelBuilder = new NotificationCompat.Builder(this, CHANNEL_ID) // For Newer API

                    .setSmallIcon(currentapiVersion)
                    .setContentTitle("FROM: "+ pickupLocation)
                    .setContentText("TO: "+ dropOffLocation)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText("TO: "+ dropOffLocation))
                    .setAutoCancel(true)
                    .setPriority(Notification.PRIORITY_HIGH)
                    .setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
                    .setWhen(System.currentTimeMillis())
                    .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                    .setContentIntent(contentIntent);

            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "BoracNotification", NotificationManager.IMPORTANCE_HIGH
            );

            channel.enableLights(true);
            channel.setLightColor(Color.BLUE);
            channel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            channel.enableVibration(true);
            channel.setDescription("FROM: "+ pickupLocation+",   TO: "+ dropOffLocation);
            mNotificationManager.createNotificationChannel(channel);
            mNotificationManager.notify((int) notificatioId, notificationChannelBuilder.build());
            turnScreenOn(0,getBaseContext());
        }else {
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)//for older API

                    .setSmallIcon(currentapiVersion)
                    .setContentTitle("FROM: "+ pickupLocation)
                    .setContentText("TO: "+ dropOffLocation)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText("TO: "+ dropOffLocation))
                    .setAutoCancel(true)
                    .setPriority(Notification.PRIORITY_HIGH)
                    .setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
                    .setWhen(System.currentTimeMillis())
                    .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                    .setContentIntent(contentIntent);

            mNotificationManager.notify((int) notificatioId, notificationBuilder.build());
            turnScreenOn(0, getBaseContext());

        }


    }

Turn Screen On :

public static void turnScreenOn(int sec, final Context context1) {

        final int seconds = sec;
        PowerManager pm = (PowerManager) context1.getSystemService(Context.POWER_SERVICE);
        boolean isScreenOn = Build.VERSION.SDK_INT>=20? pm.isInteractive() :  pm.isScreenOn();
        if (!isScreenOn) {

            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "MyLock");
            wl.acquire(seconds * 10000);
            PowerManager.WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyCpuLock");
            wl_cpu.acquire(seconds * 10000);


        }


    }

And At Last :

String orderFragment =null;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(getIntent()!=null){
            orderFragment = getIntent().getStringExtra("checkNotification");

            if(orderFragment!=null){

                if (orderFragment.equals("openOrder")) {
                    getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new OrderFragment(), null).commit();
                }else {
                    getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new MealplanFragment(), null).commit();
                }

            }else {
                getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new MealplanFragment(), null).commit();
            }

        }else {
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new MealplanFragment(), null).commit();
        }
Ad
source: stackoverflow.com
Ad