1

I'm New in Android.

When the application is open and notification came from firebase and click the notification then it open the Notification Activity but when the application is in foreground/background and notification came and click the notification then it automatically open the mainActivity.

I'm broadcasting the data from FirebaseMessagingService.class to Main Activity.

Why?

My code is below.

MainActivity

   public class MainActivity extends AppCompatActivity {

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

            LocalBroadcastManager.getInstance(this)
                    .registerReceiver(mHandler,new IntentFilter("Pakage_name_FCM-MESSAGE"));
             }

        private BroadcastReceiver mHandler = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String title = intent.getStringExtra("title");
                String message = intent.getStringExtra("message");
                Log.d("MainActivity-title",title);
                Log.d("MainActivity-message",message); 
            }
        };

        @Override
        protected void onPause() {
            super.onPause();
            // Unregister the Broadcast receiver
 LocalBroadcastManager.getInstance(this).unregisterReceiver(mHandler);
        }
    }

FirebaseNotificationIdService.Class

public class FirebaseNotificationIdService extends FirebaseInstanceIdService {

    private static final String REG_TOKEN = "REG_TOKEN";

    @Override   // each time a new created and system call this method
    public void onTokenRefresh() {
        String recent_token= FirebaseInstanceId.getInstance().getToken();
        Log.d(REG_TOKEN,recent_token);
    }
}

FirebaseMessagingService.class

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d("Remote Form",remoteMessage.getFrom());

        // check if the message contains data...
        if (remoteMessage.getData().size()> 0){
               Log.d("Message Data", "Message Data---"+remoteMessage.getData() );

            String title=remoteMessage.getData().get("title");
            String message=remoteMessage.getData().get("message");

            Intent intent = new Intent("package_name_FCM-MESSAGE");
            intent.putExtra("title",title);
            intent.putExtra("message",message);
            Log.d("INTENT-TITLE",title);
            Log.d("INTENT-MESSAGE",message);
            LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
            localBroadcastManager.sendBroadcast(intent);
        }

        //check if the message contain notification

        if (remoteMessage.getNotification()!=null){
            Log.d("Message Body","---"+remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getNotification().getBody());
        }
    }

    //Display the notification
    private void sendNotification(String body) {
        Intent intent = new Intent(this,NotificationActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,0/*Request code*/,intent,PendingIntent.FLAG_ONE_SHOT);
        //Set sound
        Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this);
        notifiBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notifiBuilder.setContentTitle("Aadhaar Update");
        notifiBuilder.setContentText(body);
        notifiBuilder.setAutoCancel(true);
        notifiBuilder.setSound(notificationSound);
        notifiBuilder.setContentIntent(pendingIntent);

        NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0/*Id of Notification*/,notifiBuilder.build());
    }
}

Minifest.xml

<activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".FirebaseNotificationIdService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
        <service android:name=".FirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <activity android:name=".NotificationActivity"></activity>
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Sujeet Kumar
  • 1,822
  • 22
  • 25

1 Answers1

2

You should use implementation of WakefulBroadcastReceiver instead of FirebaseMessagingService

   public class NotificationsReceiver extends WakefulBroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            //process data from intent
            abortBroadcast();
        }
   }

In your manifest

    <receiver
            android:name=".fcm.NotificationsReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter android:priority="999">
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
    </receiver>
Ufkoku
  • 2,384
  • 20
  • 44
  • it is helpful to me. if i want to open a specific page the what i have to do? see my question. https://stackoverflow.com/questions/44797486/open-specific-page-using-firebase-api – Harish Mahajan Jul 04 '17 at 11:25
  • `WakefulBroadcastReceiver` is deprecated as of Android O (https://developer.android.com/reference/androidx/legacy/content/WakefulBroadcastReceiver) – Nick Dec 03 '20 at 09:50