Oreo failed to post notification on channel null - android

i am trying to make notification push using FCM but every time when i am trying to send notification my Emulator always toasting failed to post notification on channel null,
i have trying many ways about posting notification on channel, but none of it is correct
i am using 'com.google.firebase:firebase-messaging:10.2.0'
here is my code
private void sendMyNotification(String message) {
int NOTIFICATION_ID = 234;
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String CHANNEL_ID = "my_channel_01";
CharSequence name = "my_channel";
String Description = "This is my channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("LALALLA")
.setContentText(message);
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
notificationManager.notify(NOTIFICATION_ID, builder.build());
} else {
//On click of notification it redirect to this Activity
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My Firebase Push notification")
.setContentText(message)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
notificationManager.notify(0, notificationBuilder.build());
}
my manifest.xml
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>

i solved my problem by upgrading my 'com.google.firebase:firebase-messaging:10.2.0' to newer version
for example , now iam using : 'com.google.firebase:firebase-messaging:11.8.0'

Related

Android 12 tap on notification not retrieve data payload on onNewIntent()

Hello Developers I am doing Android Push Notification functionality in Android 12 Native. Currently I am facing issue with not retrieve data payload when application is in background or it was closed.
Here is my notification function in MyFirebase Messaging.
#SuppressLint("WrongConstant")
public void setNotificationBuilder(Context context, String remoteMessage) {
try {
JSONObject jsonObject = new JSONObject(remoteMessage);
Intent fullScreenIntent = new Intent(context, NavigationActivity.class);
fullScreenIntent.putExtra("data", jsonObject.toString());
fullScreenIntent.putExtra("key", "credence");
fullScreenIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent fullScreenPendingIntent;
if (Build.VERSION.SDK_INT >= 31) {
fullScreenPendingIntent = PendingIntent.getActivity(context, 0, fullScreenIntent, PendingIntent.FLAG_MUTABLE);
} else {
fullScreenPendingIntent = PendingIntent.getActivity(context, 0, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "message_channel_id_1";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
#SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Credence Message", NotificationManager.IMPORTANCE_MAX);
// Configure the notification channel.
notificationChannel.setDescription("Sample Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_MAX)
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setContentIntent(fullScreenPendingIntent)
.setContentTitle(jsonObject.optString("title"))
.setContentText(jsonObject.optString("body"))
.setContentInfo(jsonObject.toString());
notificationManager.notify(1, notificationBuilder.build());
/* Intent intent1 = new Intent("intent_data");
intent1.putExtra("data", jsonObject.toString());
sendBroadcast(intent1);*/
} catch (Exception ex) {
ex.printStackTrace();
}
}
I am able to receive notification in notification tray but unable to receive data payload and perform action on it.
i refer articles and found some solution and implement another way to handle push notification.
private void openAndroid12Notification(RemoteMessage remoteMessage){
Intent TrampolineActivityIntent = new Intent(getApplicationContext(), NavigationActivity.class);
TrampolineActivityIntent.putExtra("data", "Hello World");
TrampolineActivityIntent.putExtra("key", "xyzzz");
TrampolineActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = TaskStackBuilder.create(getApplicationContext()).addNextIntentWithParentStack(TrampolineActivityIntent).getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "message_channel_id_1";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
#SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Credence Message", NotificationManager.IMPORTANCE_MAX);
// Configure the notification channel.
notificationChannel.setDescription("Sample Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_MAX)
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.addAction(R.drawable.ic_launcher, "NOTIFICATION_CLICK", pendingIntent)
.setContentIntent(pendingIntent)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setContentInfo(remoteMessage.getData().toString());
notificationManager.notify(1, notificationBuilder.build());
}
and define action in manifest file.
<activity
android:name=".NavigationActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/app_name"
android:launchMode="singleInstance"
android:exported="true"
android:screenOrientation="portrait"
android:theme="#style/MyThemeblue">
<intent-filter>
<action android:name="NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="NavigationActivity.RELOAD" />
</intent-filter>
</activity>
notification popes up but not received data payload on background, foreground or kill.
Here is my notification json format:
{
"notification": {
"title": "Test Message 1",
"body": "Test Body 1",
"click_action": "NOTIFICATION_CLICK"
},
"data": {
"requireInteraction": false,
"date": "26/12/2022",
"notificationId": "jshdsd0-dkncd-dcnkdnc-dnckdncd",
"title": "fbfbfbdk Notification",
"body": "Rdgdfer",
"content": "fgdgf.",
"click_action": "NOTIFICATION_CLICK"
},
"registration_ids": [
"token"
],
"priority": "high"
}
i want handle data payload in background and when application is closed.

Create silent notification [duplicate]

This question already has answers here:
How to show a notification without a sound java
(11 answers)
Closed 2 years ago.
I have function that creates notification. Each time it formats notification it play sound. Is it possible to create silent notification that not plays sound? How to achieve that?
public void sendNotification(String title, String message,boolean playSound) {
int NOTIFICATION_ID = 234;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String CHANNEL_ID = "my_channel_01";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
CharSequence name = "my_channel";
String Description = "This is my channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(message);
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
builder.setContentIntent(intent);
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
Except using
NotificationManager.IMPORTANCE_HIGH
use
NotificationManager.IMPORTANCE_LOW
You need to use PRIORITY_LOW for devices below Android 7 and use IMPORTANCE_LOW for Android 8 and above.
Also you can use setSound(null, null) method on your NotificationChannel(mChannel)

Android. Notification shows in status bar, but doesn't display or make sound. api 24

I have a push notifications coming into the app. However in api 24 the notification I show only appears in the status bar, but it doesn't display (like pop up), and its doesn't makes any sound.
I can't figure out why...
Here is my code:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int notificationId = 1;
String channelId = "channel-03";
String channelName = "Channel Name";
int importance = 0;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
Log.d(TAG, "android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N");
importance = NotificationManager.IMPORTANCE_HIGH;
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Log.d(TAG, "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(icon)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
Intent intent1 = new Intent(context, MainActivity.class);
stackBuilder.addNextIntent(intent1);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
notificationManager.notify(notificationId, mBuilder.build());
Needed to add to the builder:
.setDefaults(Notification.DEFAULT_ALL)

FCM notification not receiving

I have an application which will get FCM notifications.It recived fine on devices up to marshmellow.When I insatlled it on oreo device it getting toast which says notificaton channel is null.I searched on google and I found that Notification channels are required for receiving notifications on API above 26. I added a notification channel but it shows the toast again.No notification.
My AppFirebaseMessagingService
public class AppFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String title;
String description;
String click_action;
if(remoteMessage.getData()!=null) {
title = (remoteMessage.getData().get("title") == null || remoteMessage.getData().get("title").equals("")) ? "null" : remoteMessage.getData().get("title");
description = (remoteMessage.getData().get("body") == null || remoteMessage.getData().get("body").equals("")) ? "null" : remoteMessage.getData().get("body");
click_action = (remoteMessage.getData().get("click_action") == null || remoteMessage.getData().get("click_action").equals("")) ? "null" : remoteMessage.getData().get("click_action");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String id = "id_product";
// The user-visible name of the channel.
CharSequence name = "Product";
// The user-visible description of the channel.
description = "Notifications regarding our products";
int importance = NotificationManager.IMPORTANCE_MAX;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
// Configure the notification channel.
mChannel.setDescription(description);
mChannel.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.setLightColor(Color.RED);
notificationManager.createNotificationChannel(mChannel);
}
//Notification----------------------
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(AppFirebaseMessagingService.this);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle(title);
mBuilder.setContentText(description);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
SharedPreferences sharedpreferences = getSharedPreferences(Common.preferenceName, Context.MODE_PRIVATE);
String RoleCSV=sharedpreferences.getString(Common.roleCSV,"");
}
}
}
My Androidmanifest
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<service android:name=".AppFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".AppFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity
android:name=".Login"
android:configChanges="orientation|screenSize"
android:screenOrientation="portrait"
android:theme="#style/Theme.Design.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomeScreen"
android:configChanges="orientation|screenSize" />
<activity
</application>
I write the piece of code for checking if the device is oreo
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
NotificationManager notificationManager1 = (NotificationManager)
getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager1.createNotificationChannel(notificationChannel);
}
mNotification = builder
.setLargeIcon(image)/*Notification icon image*/
.setContentText(messageBody)
.setSmallIcon(R.drawable.dhlone)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image).bigLargeIcon(image))/*Notification with Image*/
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setBadgeIconType(R.drawable.dhlone)
.setAutoCancel(true)
.setSmallIcon(getNotificationIcon())
.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext()
.getResources(),R.drawable.dhlone))
.build();
notificationManager1.notify(/*notification id*/0, mNotification);
private fun createNotification(title: String, message: String) {
val resultIntent = Intent(this, LoginActivity::class.java)
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val mBuilder = NotificationCompat.Builder(this)
mBuilder.setSmallIcon(R.drawable.ic_stat_name)
mBuilder.setContentTitle(title)
.setContentText(message)
.setAutoCancel(false)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent)
val mNotificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
val importance = NotificationManager.IMPORTANCE_HIGH
val notificationChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableVibration(true)
notificationChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID)
mNotificationManager.createNotificationChannel(notificationChannel);
}
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
}

push Notification in android with android studio

I do the next for call a Notification on android studio
when I send a message out of the app, only show the Icon and the text, but does not play sound or vibration
when I am inside the app, play sound and vibrate.
Any help?
I have on manifest
Method for show notification:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
//Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Uri defaultSoundUri = Uri.parse("android.resource://"+this.getPackageName()+"/"+R.raw.power);
//Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification.Builder notificationBuilder = new Notification.Builder(this)
.setSmallIcon(R.mipmap.colantaico)
.setContentTitle(Titulo)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.setLights(Color.RED, 3000, 3000)
.setContentIntent(pendingIntent);
Notification mNotification = notificationBuilder.build();
mNotification.flags |= Notification.FLAG_INSISTENT;
mNotification.sound = defaultSoundUri;
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, mNotification);
In the Manifest
<uses-permission android:name="android.permission.VIBRATE" />
<service
android:name=".RecibidorNotificacionesFCM">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
private static final int NOTIFICATION_ID = 1;
Intent openIntent = new Intent(this, MainActivity.class);;
PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
openIntent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setDefaults(Notification.DEFAULT_ALL)
.setVibrate(new long[]{100, 250, 100, 250, 100, 250})
.setAutoCancel(true)
.setColor(this.getResources().getColor(R.color.activity_toolbar_color))
.setContentTitle("Test Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(Html.fromHtml("Notification text")))
.setPriority(Notification.PRIORITY_MAX)
.setContentText(Html.fromHtml("Notification text")))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setSmallIcon(R.drawable.notification_icon1);
} else {
mBuilder.setSmallIcon(R.drawable.notification_icon);
}
mBuilder.setContentIntent(contentIntent);
mBuilder.setDeleteIntent(LocalNotificationEventReceiver.getDeleteIntent(this));
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = mBuilder.build();
notificationManager.notify(NOTIFICATION_ID, notification);

Categories

Resources