Reputation: 181
Push notification is not working in lollipop after changing my target SDK to 26, my current minimum SDK is 19. It was working fine before. But after changing to 26, it's now working only in O, N, and M. Please suggest me a solution. Thanks in advance.
Below is my class which extends FirebaseMessaging Service
FireMsgService.java
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// super.onMessageReceived(remoteMessage);
Log.e("Msg", "Message received [" + remoteMessage + "]");
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1410,
intent, PendingIntent.FLAG_ONE_SHOT);
context = getApplicationContext();
String message = "";
String type = "";
try {
JSONObject jsonObject = new JSONObject(remoteMessage.getData());
if (jsonObject.has("id"))
message = (String) jsonObject.get("id");
if (jsonObject.has("type"))
type = (String) jsonObject.get("type");
if (jsonObject.has("room_id"))
message = (String) jsonObject.get("room_id");
if (jsonObject.has("type"))
type = (String) jsonObject.get("type");
saveTypeId(type,message);
if (jsonObject.has("badge")) {
badge = (int) jsonObject.get("badge");
Log.d("BadgeCount",badge+"");
}
Log.e("","------> "+message);
} catch (JSONException e) {
e.printStackTrace();
}
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(this)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setNumber(badge);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setSmallIcon(R.drawable.ic_new_launcher)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ic_launcher))
.setColor(ContextCompat.getColor(context,R.color.blue));
} else {
notificationBuilder.setSmallIcon(R.drawable.ic_launcher);
}
notificationBuilder.setAutoCancel(true)
.setVibrate(new long[0])
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1410, notificationBuilder.build());
}
Here is my build.gradle file
build.gradle
android {
compileSdkVersion 26
buildToolsVersion '26.0.0'
defaultConfig {
applicationId "com.abc.xyz"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
multiDexEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:cardview-v7:26.1.0'
compile 'com.android.support:design:26.1.0'
compile 'com.android.support.constraint:constraint-layout:1.1.3'
compile 'com.google.android.gms:play-services-location:11.0.4'
compile 'com.google.android.gms:play-services-maps:11.0.4'
compile 'com.google.android.gms:play-services-auth:11.0.4'
compile 'com.google.android.gms:play-services-places:11.0.4'
compile 'com.google.firebase:firebase-messaging:11.0.4'
compile 'com.google.firebase:firebase-messaging:11.0.4'
}
apply plugin: 'com.google.gms.google-services'
Upvotes: 0
Views: 635
Reputation: 11
Oreo (API 26) Version since, You must be using NotificationChannel.
if before version used. next code Reference. My Code is
Oreo (API 26) Version since use Channel.
Oreo (API 26) Version before not use Channel.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(
"channel", "channel", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(mChannel);
notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), mChannel.getId());
}else {
notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
}
Thank.
Upvotes: 1
Reputation: 4007
Since API 26, you have to specify a ChannelId
to the Notification.Builder
constructor : https://developer.android.com/reference/android/app/Notification.Builder.html#Notification.Builder(android.content.Context)
More information about Channels : https://developer.android.com/training/notify-user/channels
Upvotes: 1