0

I am setting up firebase push notification, and as per the documentation I did everything I guess

So what I did is from Tools > Firebase > Cloud Messaging added Dependencies see my gradle

implementation 'com.google.firebase:firebase-core:11.8.0'
implementation 'com.google.firebase:firebase-messaging:11.8.0'
implementation 'com.google.android.gms:play-services-ads:11.8.0'

After this I created a Class MyyFirebaseMessagingService and extended FirebaseMessagingService see below

public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    Log.d("RefreshedToken", "onMessageReceived: " + remoteMessage.getNotification().getBody());
    showNotification(remoteMessage.getNotification().getBody());
}

private void showNotification(String message) {
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
    Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.drawable.logo)
            .setContentTitle("Quotes App")
            .setContentText(message)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .build();

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(0, notification);
}}

After this I addeed Service in the manifest

<service
        android:name=".MyFirebaseMessagingService"
        android:enabled="true"
        android:exported="true"
        >
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
  </service>

And now when I try to send notification I dont receive it. What am I doing wrong

Shantanu Patil
  • 43
  • 2
  • 11

2 Answers2

2

Create a channel and set the importance

Before you can deliver the notification on Android 8.0 and higher, you must register your app's notification channel with the system by passing an instance of NotificationChannel to createNotificationChannel().

As the documentation say, you need to add a notification channel in android 8 and higher so the system shows your notification.

private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}
medyas
  • 1,166
  • 13
  • 21
1
  1. First of all, you need to create notification channel...and channel id should be between 1 to 10.

  2. Secondly, upgrade to the latest version of libraries in app/gradle.build

  3. If app is in the foreground (visible to user), onMessageReceived() is called.

  4. If app is in the background (not visible to user), then, onMessageReceived() isn't called and notification is automatically shown to user, without you manually showing them...and data values are passed as intent extras to you launcher activity when clicked.

touhid udoy
  • 4,005
  • 2
  • 18
  • 31