简体   繁体   中英

Android Amazon SNS issue (GCM)

I have an issue related with Amazon SNS service in my Android app. I'm using Amazon SNS push service in it. Please see this link http://docs.aws.amazon.com/sns/latest/dg/SNSMobilePush.html for more details about Amazon SNS service.

I implemented it like in Amazon samples http://docs.aws.amazon.com/sns/latest/dg/mobile-push-gcm.html and it worked fine for me for a some time.

But recently some users which use Android 4.0.3 or 4.0.4 have reported me a report with incorrect working push service. I started investigate this issue and found a strange behaviour of this: sometimes my push receiver takes push with message - "unregistered=my.package.name" instead of real message which I pushed.

Here is my code of AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.package"
    android:versionCode="14"
    android:versionName="1.49" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="18" />

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

    <permission android:name="my.package.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="my.package.permission.C2D_MESSAGE" />

    <application
        android:name="com.test.package.AppClass"
        android:allowBackup="true"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <receiver
            android:name="com.test.package.ExternalReceiver"
            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" />
                <action android:name="com.google.android.c2dm.intent.REGISTER" />

                <category android:name="com.test.package" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

Here is my ExternalReceiver:

    public class ExternalReceiver extends BroadcastReceiver {
    private static final String ACTION_REGISTRATION =  "com.google.android.c2dm.intent.REGISTRATION";
    private static final String ACTION_RECEIVE = "com.google.android.c2dm.intent.RECEIVE";
    private static final String UNREGISTERED = "unregistered";

    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
            Bundle extras = intent.getExtras();

            String message = "";
            String action = intent.getAction();

            if (extras != null) {
                for (String key : extras.keySet()) {
                    message += key + "=" + extras.getString(key) + "\n";
                }
            }

            if (action.equalsIgnoreCase(ACTION_REGISTRATION)) {
                Log.i(TAG, message);
            } else if (action.equalsIgnoreCase(ACTION_RECEIVE)) {
                String pushText = intent.getStringExtra("default");
                processPush(pushText, context);
            }
        }
    }
}

And this is a LogCat messages which I have when I pushed to already registered device some message:

12-20 13:32:33.683: I/Test ExtrernalReceiver(5353): registration_id=APA91bFQymRS8NOPgHDlFicpQIXDwvgWQR6_CVYC1Le-Cmhl8uTzPDPVbe1yjjKdQjMsilo4XGImAiX8ORnkZiySjbwgTmzC7lC8T3Plch1m0faKiiqc6hl75msTBmMIBhtLtKdtz_R6CY1yjygvyWplTh_yq04tCaOKCfcDcsYwYbQdmscyfUA
12-20 13:32:49.693: I/Test ExtrernalReceiver(5353): registration_id=APA91bFQymRS8NOPgHDlFicpQIXDwvgWQR6_CVYC1Le-Cmhl8uTzPDPVbe1yjjKdQjMsilo4XGImAiX8ORnkZiySjbwgTmzC7lC8T3Plch1m0faKiiqc6hl75msTBmMIBhtLtKdtz_R6CY1yjygvyWplTh_yq04tCaOKCfcDcsYwYbQdmscyfUA
12-20 13:33:19.763: I/Test ExtrernalReceiver(5353): unregistered=my.package

Your receiver entry if you manifest appears a bit off. The intent filter category tag here needs to match your application package (see 2nd line package="my.package" ). One these package names is wrong.

    <receiver
        android:name="com.test.package.ExternalReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <action android:name="com.google.android.c2dm.intent.REGISTER" />
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="my.package" />
        </intent-filter>
    </receiver>

if the actual package is "com.test.package", change both references

<category android:name="com.test.package" />

and

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.test.package"

I would advise here to use the updated Google Play Services example with the support library (it uses the support library WakefulBroadcastReceiver and no longer requires setting up a broadcast receiver for the following actions:

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

... as you Can register your application to GCM by :

                GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
                String regId = gcm.register(senderId);

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