I want to develop application android 8 above, when my application is killed, i cant receive the notification. But, it works fine at android 7 below or application open or application at background. How can i still get notification when my app is killed ? Thanks
My codes :
AndroidManifest.xml
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="channel-01" />
<service
android:name=".firebase.MyFirebaseMessagingService"
android:enabled="true"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
FirebaseMessaging.java
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage == null)
return;
if (remoteMessage.getData()!= null)
try {
Map<String, String> data = remoteMessage.getData();
PushNotificationModel pushNotificationModel = PushNotificationModel.fromMap(data);
Intent resultIntent = new Intent(getApplicationContext(), SplashActivity.class);
showNotificationMessage(getApplicationContext(), pushNotificationModel.title, pushNotificationModel.message, pushNotificationModel.timestamp, resultIntent, Integer.parseInt(pushNotificationModel.notifid));
} catch (Exception e) {
e.printStackTrace();
}
}
public void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent , int notifid)
{
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int notificationId = notifid;
String channelId = "channel-01";
String channelName = "Channel Name";
int importance = NotificationManager.IMPORTANCE_HIGH;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(
channelId, channelName, importance);
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.logo)
.setContentTitle(title)
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message));
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
notificationManager.notify(notificationId, mBuilder.build());
}
Postman to send notification (this Body, at Header i have authorization code and sender id)
{
"to": "token",
"priority":"high",
"data": {
"message": "Henlo",
"notifid": 2020090001,
"timestamp": "2020-09-29 17:45:22",
"title": "Hai"
}
}
Thanks :)