GCM未收到通知

时间:2015-10-01 07:01:11

标签: android android-intent google-cloud-messaging

我正在尝试按照谷歌开发者网站中的步骤整合GCM。 我收到了令牌,但我没有从服务器收到任何通知。

我有三项服务

1.MyGcmListenerService.java

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.android.gms.gcm.GcmListenerService;


public class MyGcmListenerService extends GcmListenerService {

private static final String TAG = "MyGcmListenerService";

/**
 * Called when message is received.
 *
 * @param from SenderID of the sender.
 * @param data Data bundle containing message data as key/value pairs.
 *             For Set of keys use data.keySet().
 */
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("message");
    Log.d(TAG, "From: " + from);
    Log.d(TAG, "Message: " + message);


    // [START_EXCLUDE]
    /**
     * Production applications would usually process the message here.
     * Eg: - Syncing with server.
     *     - Store message in local database.
     *     - Update UI.
     */

    /**
     * In some cases it may be useful to show a notification indicating to the user
     * that a message was received.
     */
    sendNotification(message);
    // [END_EXCLUDE]
}
// [END receive_message]

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received.
 */
private void sendNotification(String message) {
    Intent intent = new Intent(this, Home.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.uplogo)
            .setContentTitle("GCM Message")
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

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

    import android.app.IntentService;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.preference.PreferenceManager;
    import android.support.v4.content.LocalBroadcastManager;
    import android.util.Log;
    
    import com.google.android.gms.gcm.GcmPubSub;
    import com.google.android.gms.gcm.GoogleCloudMessaging;
    import com.google.android.gms.iid.InstanceID;
    
    import java.io.IOException;
    
    
    
    
    
         public class RegistrationIntentService extends IntentService {
    
         private static final String TAG = "RegIntentService";
         private static final String[] TOPICS = {"global"};
    
         public RegistrationIntentService() {
        super(TAG);
         }
    
        @Override
        protected void onHandleIntent(Intent intent) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    
        try {
            // [START register_for_gcm]
            // Initially this call goes out to the network to retrieve the token, subsequent calls
            // are local.
            // [START get_token]
            InstanceID instanceID = InstanceID.getInstance(this);
            String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
            // [END get_token]
            Log.i(TAG, "GCM Registration Token: " + token);
    
            // TODO: Implement this method to send any registration to your app's servers.
            sendRegistrationToServer(token);
    
            // Subscribe to topic channels
            subscribeTopics(token);
    
            // You should store a boolean that indicates whether the generated token has been
            // sent to your server. If the boolean is false, send the token to your server,
            // otherwise your server should have already received the token.
            sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, true).apply();
            // [END register_for_gcm]
        } catch (Exception e) {
            Log.d(TAG, "Failed to complete token refresh", e);
            // If an exception happens while fetching the new token or updating our registration data
            // on a third-party server, this ensures that we'll attempt the update at a later time.
            sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false).apply();
        }
        // Notify UI that registration has completed, so the progress indicator can be hidden.
        Intent registrationComplete = new Intent(QuickstartPreferences.REGISTRATION_COMPLETE);
        LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
    }
    
    /**
     * Persist registration to third-party servers.
     *
     * Modify this method to associate the user's GCM registration token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
    
    }
    
    /**
     * Subscribe to any GCM topics of interest, as defined by the TOPICS constant.
     *
     * @param token GCM token
     * @throws IOException if unable to reach the GCM PubSub service
     */
    // [START subscribe_topics]
    private void subscribeTopics(String token) throws IOException {
        GcmPubSub pubSub = GcmPubSub.getInstance(this);
        for (String topic : TOPICS) {
            pubSub.subscribe(token, "/topics/" + topic, null);
        }
    }
    // [END subscribe_topics]
    
      }
    
  2. MyInstanceIDListenerService

      import android.content.Intent;
      import android.content.SharedPreferences;
      import android.preference.PreferenceManager;
      import android.util.Log;
    
      import com.google.android.gms.iid.InstanceID;
      import com.google.android.gms.iid.InstanceIDListenerService;
    
      public class MyInstanceIDListenerService extends InstanceIDListenerService {
    
    private static final String TAG = "MyInstanceIDLS";
    
    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. This call is initiated by the
     * InstanceID provider.
     */
    // [START refresh_token]
    @Override
    public void onTokenRefresh() {
        // Fetch updated Instance ID token and notify our app's server of any changes (if applicable).
        Intent intent = new Intent(this, RegistrationIntentService.class);
        startService(intent);
    }
    // [END refresh_token]
    }
    
  3. 我在启动画面中这样做

    SplashScreen.java

    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                SharedPreferences sharedPreferences =
                        PreferenceManager.getDefaultSharedPreferences(context);
                boolean sentToken = sharedPreferences
                        .getBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false);
                if (sentToken) {
                   Toast.makeText(getApplicationContext(),"token sent",Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(),"token not sent",Toast.LENGTH_LONG).show();
                }
            }
        };
    

    我已经在清单中提供了所有权限。

    我的清单

    <?xml version="1.0" encoding="utf-8"?>
     <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="app.aguai.medieazy"
    android:versionCode="17"
    android:versionName="2.0" >
    
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    
    <permission
        android:name="app.aguai.medieazy.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
    
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    
    <permission android:name="app.aguai.medieazy.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="app.aguai.medieazy.permission.C2D_MESSAGE" />
    
    
    <uses-permission android:name="app.aguai.medieazy.permission.MAPS_RECEIVE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/uplogo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        tools:replace="android:icon" >
        <activity
            android:name=".activities.Login"
            android:label="@string/title_activity_login" >
        </activity>
        <activity android:name=".activities.Home" >
        </activity>
        <activity
            android:name=".activities.AddAddress"
            android:label="@string/title_activity_add_address"
            android:theme="@style/AppTheme2" >
        </activity>
        <activity
            android:name=".activities.UploadPrescription"
            android:label="@string/title_activity_upload_prescription"
            android:theme="@style/AppTheme2" >
        </activity>
        <activity
            android:name=".activities.OrderMedicines"
            android:label="@string/title_activity_order_medicines"
            android:theme="@style/AppTheme2" >
        </activity>
        <activity
            android:name=".activities.Pharmacies"
            android:label="@string/title_activity_pharmacies" >
        </activity>
    
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="@string/google_maps_key" />
    
        <activity
            android:name=".activities.PharmacyDetails"
            android:label="@string/title_activity_pharmacy_details"
            android:theme="@style/AppTheme2" >
        </activity>
        <activity
            android:name=".activities.SplashScreen"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activities.ReviewOrder"
            android:label="@string/title_activity_review_order"
            android:theme="@style/AppTheme2" >
        </activity>
        <activity
            android:name=".activities.FetchMyOrders"
            android:label="@string/title_activity_fetch_my_orders" >
        </activity>
        <activity
            android:name=".activities.MyOrders"
            android:label="@string/title_activity_my_orders" >
        </activity>
        <activity
            android:name=".activities.FetchMyOrderDetails"
            android:label="@string/title_activity_fetch_my_order_details" >
        </activity>
        <activity
            android:name=".activities.OrderDetails"
            android:label="@string/title_activity_order_details"
            android:theme="@style/AppTheme2" >
        </activity>
        <activity
            android:name=".activities.AddMeasurements"
            android:label="@string/title_activity_add_measurements"
            android:theme="@style/AppTheme2" >
        </activity>
        <activity
            android:name=".activities.ViewMeasurements"
            android:label="@string/title_activity_view_measurements"
            android:theme="@style/AppTheme2" >
        </activity>
        <activity
            android:name=".activities.EditProfile2"
            android:label="@string/title_activity_edit_profile2"
            android:theme="@style/AppTheme2" >
        </activity>
        <activity
            android:name=".activities.CircleOfCare"
            android:label="@string/title_activity_circle_of_care"
            android:theme="@style/AppTheme2" >
        </activity>
        <activity
            android:name=".activities.Adherence"
            android:label="@string/title_activity_adherence"
            android:theme="@style/AppTheme2" >
        </activity>
        <activity
            android:name=".activities.Test"
            android:label="@string/title_activity_test" >
        </activity>
        <activity
            android:name=".activities.MyMedications"
            android:label="@string/title_activity_my_medications"
            android:theme="@style/AppTheme2" >
        </activity>
        <activity
            android:name=".activities.AddMedicine"
            android:label="@string/title_activity_add_medicine"
            android:theme="@style/AppTheme2" >
        </activity>
        <activity
            android:name=".activities.ReminderPopUp"
            android:label="@string/title_activity_reminder_pop_up"
            android:theme="@style/AppTheme2" >
        </activity>
    
        <service
            android:name=".models.RemindService"
            android:enabled="true" >
            <intent-filter>
                <action android:name="app.aguai.medieazy.models.START_SERVICE" />
            </intent-filter>
        </service>
        <service
            android:name=".models.SnoozeService"
            android:enabled="true" >
            <intent-filter>
                <action android:name="app.aguai.medieazy.models.START_SERVICE" />
            </intent-filter>
        </service>
    
        <activity
            android:name=".activities.SnoozePopUp"
            android:label="@string/title_activity_snooze_pop_up" >
        </activity>
        <activity
            android:name=".activities.MedicineDetails"
            android:label="@string/title_activity_medicine_details"
            android:theme="@style/AppTheme2" >
        </activity>
    
        <activity
            android:name=".activities.Signup"
            android:label="@string/title_activity_signup" >
        </activity>
        <activity
            android:name=".activities.ReorderDetails"
            android:label="@string/title_activity_reorder_details"
            android:theme="@style/AppTheme2">
        </activity>
    
        <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="app.aguai.medieazy" />
            </intent-filter>
        </receiver>
    
    
        <service
            android:name="app.aguai.medieazy.GCM.MyGcmListenerService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <!-- [END gcm_listener] -->
        <!-- [START instanceId_listener] -->
        <service
            android:name="app.aguai.medieazy.GCM.MyInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID"/>
            </intent-filter>
        </service>
        <!-- [END instanceId_listener] -->
        <service
            android:name="app.aguai.medieazy.GCM.RegistrationIntentService"
            android:exported="false">
        </service>
    </application>
    

    请帮忙。

1 个答案:

答案 0 :(得分:3)

我明白了。 当我尝试其他手机时,它有效。 实际上,我的手机是Jellybean,在pre-kitkat版本中,我们需要在清单中添加这一行

<action android:name="com.google.android.c2dm.intent.REGISTRATION" />

当我这样做时,它有效。