Search code examples
androidpush-notificationfirebasefirebase-cloud-messagingfirebase-notifications

Unable to get Firebase working for Notifications


Below I have explained my sample and have given minimal code to help understand the issue.

To my gradle file, at the end

apply plugin: 'com.google.gms.google-services'

Inside my dependencies { } I have added

 compile 'com.google.firebase:firebase-messaging:9.2.0'

I also have the following in dependencies { }

compile 'com.google.android.gms:play-services:9.2.0'
compile 'com.google.android.gms:play-services-maps:9.2.0'
compile 'com.google.android.gms:play-services-ads:9.2.0'
compile 'com.google.android.gms:play-services-auth:9.2.0'
compile 'com.google.android.gms:play-services-gcm:9.2.0'
compile 'com.google.android.gms:play-services-analytics:9.2.0'
compile 'com.google.android.gms:play-services-location:9.2.0'
compile 'com.google.maps.android:android-maps-utils:0.4.+'

Manifest I have

     <!-- Google Cloud Messaging -->
        <uses-permission
            android:name="com.mcruiseon.buseeta.permission.C2D_MESSAGE"
            android:protectionLevel="signature"/>
        <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>

<service android:name=".support.NotificationIncoming">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
        <service android:name=".support.NotificationsIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>

NotificationIncoming.java has

public class NotificationIncoming extends FirebaseMessagingService {


    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d(God.LOG_TAG, "From: " + remoteMessage.getFrom());
        Log.d(God.LOG_TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
        sendNotification(remoteMessage.getNotification().getBody());
    }

    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MyBookings.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);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                //.setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("FCM Message")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

In my NotificationIDService

public class NotificationsIDService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {

        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(God.LOG_TAG, "Refreshed token: " + refreshedToken);
        sendRegistrationToServer(refreshedToken);
    }
    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
    }
}

On the Firebase I have done the following

  1. Created a project
  2. Added a SHA1 finger print
  3. Downloaded the google-services.json and copied it to app folder.

I built a signed application from the same keystore as the SHA1. And ran the application. I then sent messages from Firebase console.

No luck. What am I missing ? Something really basic I think.

EDIT

For all those reading this, the above worked perfectly for me.


Solution

  • Without any change, the above setup / code started to work, and I received these notifications late.

    @shadab-ansari, yes I did add the app from the dropdown. And I have not removed the permissions from the manifest :), its working now, I am not touching it.