首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >GCM onReceive方法不会在安卓4.0.3上触发,但可以在4.0.3以上的操作系统上使用

GCM onReceive方法不会在安卓4.0.3上触发,但可以在4.0.3以上的操作系统上使用
EN

Stack Overflow用户
提问于 2013-10-18 17:31:22
回答 2查看 1.1K关注 0票数 1

我正在写一个带有GCM推送通知的安卓应用程序(minSDK="14")。App在安卓4.0.3以上的版本(接收推送通知)上运行良好,但在4.0.3版本的app上根本不会收到任何通知。

已检查:

GcmBroadcastReceiver onReceive method not fired on Android 4.0.3

GcmBroadcastReceiver not fired on Android 4.0.3

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

EN

回答 2

Stack Overflow用户

发布于 2013-10-18 18:06:25

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

公共类GCMIntentService扩展了GCMBaseIntentService {

代码语言:javascript
运行
复制
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"));
}

}

在清单中,将这些行添加到应用程序中:

代码语言:javascript
运行
复制
        <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" />

和结束

代码语言:javascript
运行
复制
    public class GCMReceiver extends GCMBroadcastReceiver
{
    @Override
    protected String getGCMIntentServiceClassName(Context context) { 
        Log.i("Receiver", "renaming GCM service");
        return "my.package.name.GCMIntentService";
        }
}

希望能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2015-11-23 17:01:46

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19446208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档