收不到使用GCM的推送通知

3
我正在使用GCM实现推送通知。我已经成功获取到令牌和实例ID,服务器发送通知也返回成功,但我无法在手机上接收通知。请帮忙解决!!!
以下是我的代码
Android清单:
 <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.appname"
        android:versionCode="1"
        android:versionName="1.0">

        <uses-sdk
            android:minSdkVersion="14"
            android:targetSdkVersion="21" />

        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

        <!-- [START gcm_permission] -->
        <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
        <uses-permission android:name="android.permission.WAKE_LOCK" />
        <!-- [END gcm_permission] -->

        <permission android:name="com.example.appname.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />
        <uses-permission android:name="com.example.appname.permission.C2D_MESSAGE" />


        <supports-screens
            android:anyDensity="true"
            android:largeScreens="true"
            android:normalScreens="true"
            android:smallScreens="true" />



        <application
            android:allowBackup="true"
            android:icon="@drawable/appicon"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.NoActionBar">
            <activity
                android:name=".Outlet.Activities.SplashActivity"
                android:label="@string/app_name"
                android:screenOrientation="portrait">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

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

                <intent-filter>
                    <action android:name="gcm_test_app_notification_click_action"/>
                    <category android:name="android.intent.category.DEFAULT"/>
                </intent-filter>
            </activity>

            <activity
                android:name=".Outlet.Activities.LoginActivity"
                android:screenOrientation="portrait" />



            <service
                android:name="com.paypal.android.sdk.payments.PayPalService"
                android:exported="false" />

            <activity android:name="com.paypal.android.sdk.payments.PaymentActivity" />
            <activity android:name="com.paypal.android.sdk.payments.LoginActivity" />
            <activity android:name="com.paypal.android.sdk.payments.PaymentMethodActivity" />
            <activity android:name="com.paypal.android.sdk.payments.PaymentConfirmActivity" />
            <activity
                android:name="io.card.payment.CardIOActivity"
                android:configChanges="keyboardHidden|orientation" />
            <activity android:name="io.card.payment.DataEntryActivity" />

            <!-- [START gcm_receiver] -->
            <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" />
                    <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                    <category android:name="com.example.appname" />
                    <!--<category android:name="gcm.play.android.samples.com.gcmquickstart" />-->
                </intent-filter>
            </receiver>
            <!-- [END gcm_receiver] -->

            <!-- [START gcm_listener] -->
            <service
                android:name="com.example.appname.CommonFiles.Push.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="com.example.appname.CommonFiles.Push.MyInstanceIDListenerService"
                android:exported="false">
                <intent-filter>
                    <action android:name="com.google.android.gms.iid.InstanceID"/>
                </intent-filter>
            </service>
            <!-- [END instanceId_listener] -->
            <service
                android:name="com.example.appname.CommonFiles.Push.RegistrationIntentService"
                android:exported="false">
            </service>

        </application>

    </manifest>

GCMIntentservice 类:
public class MyGcmListenerService extends GcmListenerService {

    private static final String TAG = "MyGcmListenerService";

    public static final int MESSAGE_NOTIFICATION_ID = 435345;

    /**
     * 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);

        if (from.startsWith("/topics/")) {
            // message received from some topic.

        } else {
            // normal downstream 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, LoginActivity.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("GCM Message")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

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

RegisterIntentService:-

public class RegistrationIntentService extends IntentService {

    private static final String TAG = "RegIntentService";
    private static final String[] TOPICS = {"global"};

    UserSession userSession;

    public RegistrationIntentService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        userSession=new UserSession(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);
            // R.string.gcm_defaultSenderId (the Sender ID) is typically derived from google-services.json.
            // See https://developers.google.com/cloud-messaging/android/start for details on this file.
            String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

            userSession.saveGCMToken(token);

            // [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.

        GCMHelper gcmHelper = new GCMHelper(this);

        gcmHelper.checkRegisterDevice();
    }

    /**
     * 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]

}

1
你在onMessageReceived()方法中收到消息了吗? - Pankaj
你来自乌代布尔吗? - Pankaj
@Clairvoyant 不会帮助我,除非我来自乌代布尔??? - Neha Tyagi
请在您的清单文件中添加以下内容以使用自定义权限:<!-- 创建自定义权限,以便只有此应用程序可以接收其消息。 --> <permission android:name="YOUR_PACKAGE_NAME.permission.C2D_MESSAGE" android:protectionLevel="signature" /><uses-permission android:name="YOUR_PACKAGE_NAME.permission.C2D_MESSAGE" /> <!-- 此应用程序具有注册和接收数据消息的权限。--> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> - Pankaj
让我们在聊天中继续这个讨论 - Pankaj
显示剩余2条评论
3个回答

1

你需要在清单文件中编写

<permission android:name="com.example.gcm.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />

其中com.example是您的包名。

但您的应用程序包名是com.example.appname

因此,您需要编写

<permission android:name="com.example.appname.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />
 <uses-permission android:name="com.example.appname.permission.C2D_MESSAGE" />

应该是"com.example.appname.gcm.permission.C2D_MESSAGE"还是"com.example.appname.permission.C2D_MESSAGE"? - Neha Tyagi
使用GCM还是不使用GCM? - Neha Tyagi
编辑你的回答,我会给你点赞...但这只解决了我的一半问题。 - Neha Tyagi
有什么问题? - Mayuri Joshi
问题在于应该是 packagename.permission 而不是 gcm。 - Neha Tyagi

1
将权限更改为以下内容并检查。
<!-- Creates a custom permission so only this app can receive its messages. --> 
    <permission android:name="YOUR_PACKAGE_NAME.permission.C2D_MESSAGE" android:protectionLevel="signature" /> 
    <uses-permission android:name="YOUR_PACKAGE_NAME.permission.C2D_MESSAGE" /> 
    <!-- This app has permission to register and receive data message. -->
     <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

从您提到的“服务”中删除“清单”中的“导出标记”。 - Pankaj
我写的代码可以工作..不需要移除任何内容。 - Neha Tyagi
让我们在聊天中继续这个讨论:点击此处进入聊天室 - Pankaj

1
我已经找到了解决我的问题的方法。
首先,我纠正了我的应用程序中的这些权限,尽管新版本不会出现问题。
 <permission
    android:name="com.example.appname.permission.C2D_MESSAGE"
                                android:protectionLevel="signature" />

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

第二,问题在于Wi-Fi隐私。当我使用公司的Wi-Fi时,手机无法接收信息,因为GCM服务器端口被阻止。
您可以使用以下链接有效地实现GCM:

https://developers.google.com/cloud-messaging/ https://github.com/codepath/android_guides/wiki/Google-Cloud-Messaging

快乐编程 :)


不错的发现。我听说你可以阻止GCM服务器端口。很酷。 - Pankaj
谢谢。如果我没有解决这个问题,我就永远不会收到通知了。 - Neha Tyagi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接