I am having an issue with FireBase Cloud Messaging in which I get the Token from the device and send the notification test through the Google Firebase notification console h
maybe it is from the connection I changed the connection from Ethernet to wireless the it worked without doing anything else
I had the same problem and it is solved by defining enabled, exported to true in my service
<service
android:name=".MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
I faced the same problem but I had still had some debris in my manifest from the old GCM. When I took the following permission out of my manifest it fixed the error. com.google.android.c2dm.permission.RECEIVE
When you copying your device token, it might copying the space, double check you have copied the correct token
As if you want your app to run in > 24 versions too, follow these:
1.Add this in your strings.xml
< string name="default_notification_channel_id" translatable="false"> fcm_default_channel
< meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id" />
3.for creating notifications (with images) use this method where you are handling notifications, if directly then add in the firebasemessaging service or if you are using some util class then add in that util class :
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public static void createBigNotification(Bitmap bitmap, int icon, String message, Uri alarmSound) {
final int NOTIFY_ID = 0; // ID of notification
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
String id = mContext.getString(R.string.default_notification_channel_id); // default_channel_id
String title = mContext.getString(R.string.app_name);
Intent intent;
NotificationCompat.Builder builder;
if (notificationManager == null) {
notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
}
PendingIntent resultPendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = notificationManager.getNotificationChannel(id);
if (mChannel == null) {
mChannel = new NotificationChannel(id, title, importance);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(mChannel);
}
builder = new NotificationCompat.Builder(mContext, id);
intent = new Intent(mContext, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
resultPendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.setBigContentTitle(title);
bigPictureStyle.setSummaryText(Html.fromHtml(message).toString());
bigPictureStyle.bigPicture(bitmap);
builder.setContentTitle(title) // required
.setSmallIcon(R.drawable.app_icon) // required
.setContentText(message) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setSound(alarmSound)
.setStyle(bigPictureStyle)
.setContentIntent(resultPendingIntent)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setTicker(title)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
} else {
builder = new NotificationCompat.Builder(mContext, id);
intent = new Intent(mContext, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
resultPendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.setBigContentTitle(title);
bigPictureStyle.setSummaryText(Html.fromHtml(message).toString());
bigPictureStyle.bigPicture(bitmap);
builder.setContentTitle(title) // required
.setSmallIcon(R.drawable.app_icon) // required
.setContentText(message) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setSound(alarmSound)
.setStyle(bigPictureStyle)
.setContentIntent(resultPendingIntent)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setTicker(title)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
.setPriority(Notification.PRIORITY_HIGH);
}
Notification notification = builder.build();
notificationManager.notify(NOTIFY_ID, notification);
}
Follow these steps and your notification will come in the notification tray.
Call super.OnMessageReceived()
in the Overriden method. This worked for me!
Finally!