Chris Palma
Chris Palma

Reputation: 223

android - Firebase Notification Push Notification not working

UPDATE

Unfortunately, I didn't solve this issue and what I did is create a new project. Fortunately my new project works. One thing I noticed from my newly created project, when I am sending notification messages, some messages didn't arrive to my device. So I think its my Internet connection problem (my idea only).

I am trying to implement basic receiving of Push Notifications using Firebase. I based this tutorial:

https://www.codementor.io/android/tutorial/send-push-notifications-to-android-with-firebase

My Problem is I don't receive any messages at all. I have sent more than 5 messages using the Firebase Console and still nothing happens.

Here is my implementation of FirebaseInstanceIdService:

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

public static final String debugTag = "MyFirebaseIIDService";

@Override
public void onTokenRefresh() {
    super.onTokenRefresh();

    //Getting registration token
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();

    //Displaying token in logcat
    Log.e(debugTag, "Refreshed token: " + refreshedToken);

}

private void sendRegistrationToServer(String token) {
    //You can implement this method to store the token on your server
    //Not required for current project
}

}

My FirebaseMessagingService implementation:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

public static final String debugTag = "MyFirebaseApp";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    Log.d(debugTag, "Notification Received!");

    //Calling method to generate notification
    sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());

}

//This method is only generating push notification
private void sendNotification(String title, String messageBody) {
    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 defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
}

}

My Manifest file:

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".activities.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

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

    <service android:name=".tokenservice.MyFirebaseInstanceIdService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>

</application>

What am I doing wrong? Or what am I lacking to implement?

I am really needing some help with this one. Any help will be greatly appreciated. Thank you very much!

Upvotes: 2

Views: 10639

Answers (2)

DV Singh
DV Singh

Reputation: 1088

onRefreshToken() deprecated, Use onNewToken(String s)

check here

Upvotes: 0

Gg M
Gg M

Reputation: 396

In my case, every thing was OK,but the app's notification channels was inactive. so make sure your notification channel is active in setting.

may help some one.

Upvotes: 1

Related Questions