My FireBaseMessaging.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
//private static final String TAG = "AbsWorkoutFCM";
private static final String TAG = "faceenhancer";
//String app_banner_url, app_package_name;
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
String app_banner_url= null, app_package_name = null;
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
Map<String, String> data = remoteMessage.getData();
if (data.containsKey(APP_PACKAGE_NAME)) {
app_package_name = data.get(APP_PACKAGE_NAME);
app_banner_url = data.get(APP_BANNER_URL);
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
String body = remoteMessage.getNotification().getBody();
String title = remoteMessage.getNotification().getTitle();
//String Icon = remoteMessage.getNotification().getIcon();
sendNotification(app_package_name, app_banner_url, body, title);
}
// [END receive_message]
/**
* Create and show a simple notification containing the received FCM message.
*
* @param package_name FCM message body received.
*/
private void sendNotification(String package_name, String app_banner_url, String body, String title) {
Intent intent = new Intent(this, LaunchActivity.class);
intent.putExtra(APP_PACKAGE_NAME, package_name);
intent.putExtra(APP_BANNER_URL, app_banner_url);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)//ic_stat_ic_notification
.setSmallIcon(R.mipmap.fcm_faceenhancer)//tatto_icon
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
And service in Manifest file as
<service
> android:name="com.faceenhancer.android.fcm.MyFirebaseMessagingService"
> android:enabled="true"
> android:exported="true">
> <intent-filter>
> <action android:name="com.google.firebase.MESSAGING_EVENT" />
> </intent-filter>
</service
When sent notification message from console getting this exception in logcat.. https://i.stack.imgur.com/sOJXI.png
Exception java.lang.NoClassDefFoundError:
at com.google.firebase.messaging.zzc.<init> (com.google.firebase:firebase-messaging@@20.0.1:3)
at com.google.firebase.messaging.FirebaseMessagingService.<init> (com.google.firebase:firebase-messaging@@20.0.1:1)
at com.faceenhancer.android.fcm.MyFirebaseMessagingService.<init> (MyFirebaseMessagingService.java:26)
at java.lang.Class.newInstance
at android.app.AppComponentFactory.instantiateService (AppComponentFactory.java:129)
at androidx.core.app.CoreComponentFactory.instantiateService (CoreComponentFactory.java:75)
at android.app.ActivityThread.handleCreateService (ActivityThread.java:4299)
at android.app.ActivityThread.access$1700 (ActivityThread.java:252)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2018)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loop (Looper.java:250)
at android.app.ActivityThread.main (ActivityThread.java:7886)
at java.lang.reflect.Method.invoke
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:970)
This error appeared when notification is sent from firebase console and app crashed
Please help after sending message from console getting