5

Okay so what I am doing right now is getting a push notification through FCM that's been going well. Now I'm able to change the activity when application is on foreground, but how do I change it when I tap the notification in notification panel? Need help.

My Code:

public void showNotificationMessage(final String title, final String message, final String timeStamp, Intent intent, String imageUrl) {

        // Check for empty push message
        if (TextUtils.isEmpty(message))
            return;

        // notification icon
        final int icon = R.mipmap.ic_launcher;

        // on click activity for the notification !!!!!!!!!!
        intent = new Intent(Intent.ACTION_MAIN);
        intent.setClass(mContext, TestActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        final PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        mContext,
                        0,
                        intent,
                        PendingIntent.FLAG_CANCEL_CURRENT
                );

        final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                mContext);
AL.
  • 36,815
  • 10
  • 142
  • 281
Ahsan Arif
  • 65
  • 1
  • 1
  • 7

5 Answers5

9
private void sendNotification(String msg) {
    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    long notificatioId = System.currentTimeMillis();

    Intent intent = new Intent(getApplicationContext(), TestActivity.class); // Here pass your activity where you want to redirect.

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(this, (int) (Math.random() * 100), intent, 0);

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

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(currentapiVersion)
            .setContentTitle(this.getResources().getString(R.string.app_name))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg)
            .setAutoCancel(true)
            .setPriority(Notification.PRIORITY_HIGH)
            .setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
            .setContentIntent(contentIntent);
    mNotificationManager.notify((int) notificatioId, notificationBuilder.build());
}
Bhavnik
  • 2,020
  • 14
  • 21
3

I have used in this manner to start a specific activity:

FireBaseMessagingService.java

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //The message will contain the Push Message
        String message = remoteMessage.getData().get("message");
        //imageUri will contain URL of the image to be displayed with Notification
        String imageUri = remoteMessage.getData().get("image");
        //title for the notification.
        String title = remoteMessage.getData().get("title");
        //action string to perform the action e.g. open activity
        String action = remoteMessage.getData().get("click_action");
        //To get a Bitmap image from the URL received
        bitmap = getBitmapfromUrl(imageUri);
        //method for functioning the notification --->
        sendNotification(message, title, bitmap, action);
    }

 private void sendNotification(String messageBody, String title, Bitmap image, String action) {
            Intent intent = new Intent(this, SpecificActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("title", title);
            ByteArrayOutputStream _bs = new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.PNG, 50, _bs);
            intent.putExtra("img", image);
            intent.putExtra("msg", messageBody);
            intent.putExtra("click_action", action);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                    PendingIntent.FLAG_ONE_SHOT);

            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new 
                    NotificationCompat.Builder(this, "Default")
                    .setLargeIcon(image)/*Notification icon image*/
                    .setSmallIcon(R.mipmap.app_icon)
                    .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image))/*Notification with Image*/
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setPriority(Notification.PRIORITY_HIGH)
                    .setChannelId("Default")
                    .setVibrate(new long[]{1000, 1000})
                    .setContentIntent(pendingIntent);

            notificationBuilder.setContentTitle(title);
            notificationBuilder.setContentText(messageBody);
            notificationBuilder.setAutoCancel(true);
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

    }

After this Add the following lines in the specificActivity.java part in the AndroidManifest.xml file:

 <activity
            android:name=".SpecificActivity">
            <intent-filter>
                <action android:name="OPEN_ACTIVITY" />
 <!-- Add this OPEN_ACTIVITY string into your data payload while sending the notification from server side. -->
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

After this get the intent in the specific activity you are starting i.e. SpecificActivity.java file's onCreate() method.

if (getIntent().getExtras() != null) {
            for (String key : getIntent().getExtras().keySet()) 
                {
                String value = getIntent().getExtras().getString(key);
                if (key.equals("click_action")) {
                //perform the action you want to do with the key.
                }

After adding these you are good to check the notifications from your mobile end.

amit pandya
  • 1,384
  • 13
  • 22
1
        NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent notificationIntent = new Intent(mContext,ACTIVITY_TO_BE_DISPLAYED.class); // Replace ACTIVITY_TO_BE_DISPLAYED to Activity to which you wanna show
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent intent = PendingIntent.getActivity(mContext, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
                .setAutoCancel(true)
                .setTicker("YOUR_TICKER_MSG")
                .setSmallIcon(R.drawable.ic_notification_icon)
                .setLargeIcon(icon)
                .setContentTitle("YOUR_TITLE")
                .setContentText("YOUR_TEXT")
                .setContentIntent(intent);
        notificationManager.notify(10, builder.build());
Mukeshkumar S
  • 785
  • 1
  • 14
  • 30
0

Pass your Activity you want to open when clicked into Intent.

Intent notificationIntent = new Intent(context, XYZActivity.class);

complete code:

NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);
    
        Intent notificationIntent = new Intent(context, XYZActivity.class);
    
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    
        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);
    
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
Satyam Anand
  • 377
  • 1
  • 8
0
<!-- MainActivity is the parent for ResultActivity -->
    <activity
        android:name=".ResultActivity"
       />

Dont forget to adjust Manifest with child activity declaration

Waqas N
  • 21
  • 3