【问题标题】:don't receive notifications on android 2.2 using GoogleCloudMessaging使用 GoogleCloudMessaging 在 android 2.2 上不接收通知
【发布时间】:2013-12-08 21:37:19
【问题描述】:

我在我的项目中使用了新的 GoogleCloudMessaging API(来自 Play Services 库,因为旧的 Android 的 Google 云消息传递已被弃用)并且我有一些错误。当我从我的服务器向所有注册设备发送消息时,我的 nexus 4 (android 4.4) 收到通知消息,但我的三星 Galaxy Ace 没有收到它们,这很奇怪,因为我的项目没有异常或崩溃。

有什么想法吗? 谢谢。

Manifest.xml

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

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

    <uses-permission android:name="android.permission.INTERNET" />
    <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" />

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

    <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.providers.gsf.permission.READ_GSERVICES"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/MyTheme" >

        <service android:name=".GcmIntentService" />

        <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="com.example.gcm" />
              </intent-filter>
        </receiver>

        <activity
            android:name="ru.test.MainActivity"
            android:label="@string/app_name" 
            android:configChanges="orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

    </application>

</manifest>

GcmBroadcastReceiver.java

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub


        String regId = intent.getExtras().getString("registration_id");
           if(regId != null && !regId.equals("")) {
              /* Do what ever you want with the regId eg. send it to your server */

               Log.d(MainActivity.tag, "onReceive " + regId);
           }

        // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),
                GcmIntentService.class.getName());
        // Start the service, keeping the device awake while it is launching.
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);

    }

}

GcmIntentService.java

public class GcmIntentService extends IntentService {
    public static int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    static final String TAG = "myLogs";

    String title = "";
    String message = "";
    String subtext = "";

    public GcmIntentService() {
        super("GcmIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {  // has effect of unparcelling Bundle

             title = extras.getString("title");
             message = extras.getString("message");
             subtext = extras.getString("subtext");


            /*
             * Filter messages based on message type. Since it is likely that GCM
             * will be extended in the future with new message types, just ignore
             * any message types you're not interested in, or that you don't
             * recognize.
             */ 
            if (GoogleCloudMessaging.
                    MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                message = "Send error";
                sendNotification(title, message, subtext);
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_DELETED.equals(messageType)) {             
                message = "Deleted messages on server";
                sendNotification(title, message, subtext);
            // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_MESSAGE.equals(messageType)) {

                // Post notification of received message.
                sendNotification(title, message, subtext);
                Log.d(TAG, "Received: " + extras.toString());
            }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    // Put the message into a notification and post it.
    // This is just one simple example of what you might choose to do with
    // a GCM message.
    private void sendNotification(String title, String message, String subtext) {



        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, SplashScreen.class), 0);

        NotificationCompat.BigTextStyle bigxtstyle =
                new NotificationCompat.BigTextStyle();          
                bigxtstyle.bigText(message);               

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_small)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_large))
                .setTicker(message)
                .setContentTitle(title)
                .setDefaults(Notification.DEFAULT_ALL | Notification.FLAG_AUTO_CANCEL)
                //.setContentInfo("Content info")
                .setAutoCancel(true)
                .setSubText(subtext)
                .setWhen(System.currentTimeMillis())
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                .setContentText(message);

        mBuilder.setContentIntent(contentIntent);

        mNotificationManager.notify(++NOTIFICATION_ID, mBuilder.build());



    }
}

【问题讨论】:

  • 如果您想要一个答案而不是猜测,您应该发布您的清单和客户端代码。

标签: android push-notification google-cloud-messaging android-notifications google-play-services


【解决方案1】:

您的主包名称是 ru.test,但在 GCM 权限中您指定了 com.example.gcm 作为包名称。这就是它在 2.x Android 版本中不起作用的原因(由于某种原因,这种不匹配不会影响 4.x 版本)。

以下更改将解决您的问题:

<permission android:name="ru.test.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="ru.test.permission.C2D_MESSAGE" />

    <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="ru.test" />
          </intent-filter>
    </receiver>

我还建议您将 minSdkVersion 从 7 更改为 8,因为 API 级别 7 不支持 GCM。

【讨论】:

    【解决方案2】:

    您的 Google Play 应用程序已在 Galaxy Ace 中更新?可能这就是问题所在。

    另一个假设不会登录到设备上的任何 google 帐户。要在 4.0.4 之前的版本中接收通知,必须至少在一个方面进行登录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多