Poonam Desai
Poonam Desai

Reputation: 85

GCM Push Notification not received

I try this code for GCM push notification. In this for send notification I use this. But it shows the message like Cool! Message sent successfully check your device... But My device does not receive Notification.

Upvotes: 1

Views: 509

Answers (3)

giuseppe trubia
giuseppe trubia

Reputation: 142

You should use FCM. See

1) Add this line to build.gradle:
dependencies { compile 'com.google.firebase:firebase-messaging:9.8.0'}

2) Add this service to manifest.
<service android:name=".MyFirebaseInstanceIDService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service>
3) Add this class to your project. I suggest to you to save your FCM Token:

public class MyInstanceIDListenerService extends FirebaseInstanceIdService {


@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d("FirebaseService", "Refreshed token: " + refreshedToken);

    SharedPreferences sharedPref = getSharedPreferences("YOUR_SETTING_NAME", Context.MODE_PRIVATE);
    //There are optional steps
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("token", refreshedToken);
    //notify the token update 
    editor.putBoolean("tokenUpdate",true);
    editor.commit();
    //You can send this token to your server (if you have)
    sendServer(refreshedToken)
    }
}

4) Now register your app to https://console.firebase.google.com/. This will generate a JSON file that's you must put inside app folders.

5) For generate FCM message you have 2 possibility:
5.1) Use the default Firebase Console: https://console.firebase.google.com/project/fantamanager-2017/notification/compose
5.2) Built your server app (If you want, i can send to you my Php Server Code)

6) Add this Service to your Manifest.xml

<service android:name="FcmBroadcastReceiver">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
</service>

And create a class that can intercept the push:

 public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final ID_NOTIFICATION = ...
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

       String from,data;
       from=remoteMessage.getFrom());


        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
           data=remoteMessage.getNotification().getBody());
        }

        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
        sendNotification(data);
    }



    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.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_stat_ic_notification)
                .setContentTitle("FCM Message")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

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

Or, if you have a custom service, change the body of onMessageReceived function with this:

   Map data = message.getData();
    if (data.containsKey(YOUR_DATA_FIELD_1)) {
        String field1= data.get(YOUR_DATA_FIELD_1).toString();
        String field2= data.get(YOUR_DATA_FIELD_2).toString();
        ....
        sendNotification(field1,field2...);
        return;
    }

Upvotes: 1

Mert
Mert

Reputation: 93

Try Firebase, here is the documentation of Push-Notification https://firebase.google.com/docs/cloud-messaging/ or you can check this tutorial https://www.simplifiedcoding.net/android-push-notification-tutorial-using-firebase/

Upvotes: 1

Pritish
Pritish

Reputation: 1368

Did you try a part or whole code ? Coz last I saw , new applications for gcm are closed. Only FCM is available. So you need to register as they say , implement the json file in app and proceed. If you already had a project which implemented gcm and you tried part of this code , then please check if the key is proper and you have not copied they key from this project or something . or maybe some meta data from manifest file is missing.

Upvotes: 0

Related Questions