Hi i am working on an app that uses Firebase Cloud Messaging and i am trying to show notification when app receives a message using Firebase Cloud Messaging.
When App is Running
Now when App is closed not running in background (these are the main issues that need to be resolved and need explanation for this unnatural behaviour)
Below are the Code Snippets.
Mainfest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".Splash"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:label="@string/title_activity_login"
android:windowSoftInputMode="stateHidden" />
<activity
android:name=".SignupActivity"
android:windowSoftInputMode="stateHidden" />
<activity
android:name=".Dashboard"
android:label="@string/title_activity_dashboard" />
<activity android:name="pk.edu.kiu.jobslocatoer.NotificationActivity"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true">
<intent-filter>
<action android:name="pk.edu.kiu.jobslocatoer.MESSAGING_ACTIVTY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<receiver android:name=".receivers.NotificationReceiver">
<intent-filter>
<action android:name="pk.edu.kiu.jobslocatoer.MESSAGING_EVENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity android:name=".SettingsActivity"></activity>
<service android:name=".services.MessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".services.InstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
</application>
NotificationReceiver.java
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent notify = new Intent(context, NotificationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) SystemClock.currentThreadTimeMillis(), notify, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Titile")
.setContentText("Text")
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify((int) SystemClock.currentThreadTimeMillis(), notification);
}
}
MessagingService.java
public class MessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(GlobalConfig.TAG, GlobalConfig.getRef(this) + " notificaiton: " + remoteMessage.getNotification().getBody());
sendBroadcast(new Intent("pk.edu.kiu.jobslocatoer.MESSAGING_EVENT"));
}
}
Firebase Notification
1.If you are sending notification through firebase console,firebase will add a notification object to your message like this
{
//..
"notification" : {
"body" : "message body sample text",
"title" : "notification Tittle",
"icon" : "icon information sample"
}
//..
}
1) If your notification contains this notification payload and If your app is in background then,
2) If your notification contains this notification payload + data payload and If your app is in background then,
3) If your notification contains only data payload({data" : {"body" : "message body sample text","title" : "notification Tittle","icon" : "icon information sample"}
) and If your app is in background or foreground then,
So If you want to execute OnMessageReceived()
method everytime when you get a notification,then from server side you should always pass only data payload.Otherwise OnMessageReceived()
will not be executed and Firebase will handle the notification showing thing.