简体   繁体   中英

Got “success” without receiving any message with GCM

I've tried to send a notification from server to my app on my phone and I get "success" but my phone doesn't receive anything, I'm using Google Cloud Messaging, although I received instance ID with message in my server and use it to send a message.

    public class GCMIntentService extends GcmListenerService{


    private static final String TAG = "GCMIntentService";

    @Override
    public void onMessageReceived(String from, Bundle data) {

        String message = data.getString("message");
        Log.d(TAG, "from:" + from);
        Log.d(TAG, "message:" + message);

        sendNotification(message);
    }
     private void sendNotification(String message){
         Intent intent = new Intent(this, MainActivity.class);
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

         Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
         if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){    
             NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.logo)
                    .setContentTitle("New Message")
                    .setContentText(message)
                    .setAutoCancel(true)
                    .setSound(defaultSound)
                    .setContentIntent(pendingIntent);

            NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                    notificationManager.notify(0, notificationBuilder.build());    
        }    
    }
}

AndroidManifest

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <permission android:name="com.example.abdul_majeed.alruthea.permission.C2D_MESSAGE" android:protectionLevel="signature" />

    <uses-permission android:name="com.example.abdul_majeed.alruthea.permission.C2D_MESSAGE" />

....

    <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND">      
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.example.abdul_majeed.alruthea" />
        </intent-filter>    
    </receiver>   
    <service
        android:name=".GCMIntentService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>

======================================

Try to run in debug mode and check if the device is receiving it. You may try to test the gcm connection here: https://console.developers.google.com

Link below is the Official Google Documentation for GCM, you may try to check step by step procedure on how to setup a GCM client App: https://developers.google.com/cloud-messaging/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM