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>