Android 4.x设备接收GCM消息,但Android 2.3设备不接收

时间:2013-06-07 21:24:03

标签: android notifications push-notification google-cloud-messaging android-2.3-gingerbread

我的Android应用程序从不在2.3设备上接收GCM消息,但它在4.x设备上接收。我可以成功注册所有设备(2.3和4.x)。我认为这可能与this issue有关,但似乎我的android清单配置正确。有人能够注意我的IntentService和BroadcastReceiver,看看他们是否注意到任何问题?任何帮助将不胜感激。请注意,在我附加调试器的情况下发送通知时,永远不会为Android 2.3调用onHandeIntent()。我检查了4.x设备,它们确实触发了onHandleIntent()中的调试器。 谢谢!

Android Manfest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.package"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="my.package.matchtracker.permission.C2D_MESSAGE" />
    <permission android:name="my.package.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver
            android:name=".GcmBroadcastReceiver"
            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="my.package" />
            </intent-filter>
        </receiver>
        <service android:name=".NotificationIntentService" android:enabled="true" />
        <activity android:name="com.gigya.socialize.android.GSWebViewActivity" />
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:configChanges="orientation|screenSize"
            android:theme="@android:style/Theme.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

广播接收器:

package my.package;

import android.app.*;
import android.content.*;

public class GcmBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationIntentService.runIntentInService(context, intent);
        setResultCode(Activity.RESULT_OK);
    }
}

通知意向服务

public class NotificationIntentService extends IntentService {
    private String TAG = "NotificationIntentService";
    public NotificationIntentService() {
        super(AppConstants.GCM_SENDER_ID);
    }

    public NotificationIntentService(String name) {
        super(name);
        // TODO Auto-generated constructor stub
    }

    private static PowerManager.WakeLock sWakeLock;
    private static final Object LOCK = NotificationIntentService.class;

    static void runIntentInService(Context context, Intent intent) {
        synchronized(LOCK) {
            if (sWakeLock == null) {
                PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
                sWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "my_wakelock");
            }
        }
        sWakeLock.acquire();
        intent.setClassName(context, NotificationIntentService.class.getName());
        context.startService(intent);
    }

    public final void onHandleIntent(Intent intent) {
        try {
            String action = intent.getAction();
            if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) {
                //don't care.
            } else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) {
                handleMessage(intent);
            }
        } finally {
            synchronized(LOCK) {
                sWakeLock.release();
            }
        }
    }

    private void handleMessage(Intent intent) {
        Bundle b = intent.getExtras();
        String text = b.getString("text"),
               title = b.getString("title"),
               largeImageUrl = b.getString("largeImageUrl");
        Log.i(TAG, "Message is " + text);
        NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        Bitmap bit=null;
        if (largeImageUrl != null && !largeImageUrl.isEmpty()) {
            try{bit = BitmapFactory.decodeStream((InputStream)new URL(largeImageUrl).getContent());
            } catch (Exception e){}
        }
        NotificationCompat.Builder nc = new NotificationCompat.Builder(this)
                                            .setContentTitle(title)
                                            .setContentText(text)
                                            .setSmallIcon(R.drawable.ic_launcher)
                                            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                                            .setAutoCancel(true) //notification disappears when clicked
                                            .setContentIntent(PendingIntent.getActivity(this, 0,
                                                    new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT));
        //bit = Bitmap.createScaledBitmap(bit, android.R.dimen.notification_large_icon_width, android.R.dimen.notification_large_icon_height, true);
        if (bit != null) nc.setLargeIcon(bit);
        nm.notify(0, nc.build());
    }
}

3 个答案:

答案 0 :(得分:16)

我能看到的第一个潜在问题是:

permission元素中的包名称与uses-permission元素中的包名称不同。在我的应用程序(针对Android 2.2)中,它们完全相同。

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="my.package.matchtracker.permission.C2D_MESSAGE" />
<permission android:name="my.package.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

答案 1 :(得分:0)

如果您正在使用具有不同applicationId的产品风味,请参阅我的answer问题在Android 2.3.6 v上未收到GCM消息但在android 4.X v上正常工作

答案 2 :(得分:0)

GCM需要在设备上安装Google Play服务,但在版本2.3中,默认情况下不会安装。

相关问题