1

我正在编写一个带有 GCM 推送通知的 android 应用程序 (minSDK="14")。应用程序在高于 4.0.3(接收推送通知)的 android 上运行良好,但在 4.0.3 应用程序上根本没有收到任何通知。

已检查:
在 Android 4.0.3 上未触发 GcmBroadcastReceiver onReceive 方法,在 Android 4.0.3 上
触发 GcmBroadcastReceiver

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

4

2 回答 2

1

对于我的应用程序,我使用 GCMIntentService

公共类 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";
        }
}

希望能帮助到你。

于 2013-10-18T10:06:24.517 回答
0

使用 Google 提供的提供 GcmListenerService 类的最新机制,扩展该类并覆盖 onMessageRecieved(String from,Bundle data) 方法。

于 2015-11-23T09:01:46.447 回答