GCM onReceive方法未在Android 4.0.3上触发,但适用于4.0.3以上的操作系统

时间:2013-10-18 09:31:22

标签: android google-cloud-messaging

我正在使用GCM推送通知编写一个Android应用程序(minSDK =" 14")。应用程序在Android高于4.0.3(接收推送通知)时工作正常,但在4.0.3应用程序上根本不会收到任何通知。

已经检查过:
GcmBroadcastReceiver onReceive method not fired on Android 4.0.3GcmBroadcastReceiver not fired on Android 4.0.3

所有权限和包都正确,服务器部分正常(从GCM获得成功),但没有收到通知, 我项目中的android支持库也是' android-support-v13.jar'

1 个答案:

答案 0 :(得分:1)

对于我的应用,我使用GCMIntentService

  

public class GCMIntentService扩展GCMBaseIntentService   {

public GCMIntentService()
{
    super(senderId);
}

/**
 * Issues a notification to inform the user that server has sent a message.
 */
@SuppressLint("NewApi")
private static void generateNotification(Context context, String message)
{
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification;
    PendingIntent intent;// = YourPendingIntent;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
    {

        Notification.Builder builder = new Notification.Builder(context);
        builder.setSmallIcon(R.drawable.icon);
        builder.setTicker(message);
        builder.setWhen(System.currentTimeMillis());
        builder.setContentTitle(context.getString(R.string.app_name));
        builder.setContentText(message);
        builder.setContentIntent(intent);
        notification = new Notification.BigTextStyle(builder).bigText(message).build();
    }
    else
    {

        notification = new Notification(R.drawable.icon, message, System.currentTimeMillis());
        String title = context.getString(R.string.app_name);
        notification.setLatestEventInfo(context, title, message, intent);
    }

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
}

@Override
protected void onMessage(Context arg0, Intent arg1)
{

    Log.d("GCM", "MESSAGE RECEIVED");
    Bundle b = arg1.getExtras();
    generateNotification(   arg0, b.getString("body"));
}

}

在您的清单中添加以下行:

        <receiver
        android:name="my.package.name.GCMReceiver"
        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.name" />
        </intent-filter>
    </receiver>

    <service
        android:name=".GCMIntentService"
        android:enabled="true" />

完成

    public class GCMReceiver extends GCMBroadcastReceiver
{
    @Override
    protected String getGCMIntentServiceClassName(Context context) { 
        Log.i("Receiver", "renaming GCM service");
        return "my.package.name.GCMIntentService";
        }
}

希望它有所帮助。