当前位置: 首页 > 知识库问答 >
问题:

在设备上接收到GCM消息,但在电话重新启动后未传递到应用程序

仰英发
2023-03-14

我在android上添加了gcm通知服务,该服务运行良好,但当设备重新启动时,我看不到任何消息传递给应用程序。

我检查了打印出来的日志,它显示给我一个日志,上面写着:

"I/GCM﹕ GCM message com.package.name 0:1438085xxxxxxxxxxxxxxxxxx" 
Logger.info(TAG, "GOT PUSH NOTIFICATION");

final Session session = Session.getInstance();
if(session == null){
    Logger.error("Not Handling Message.\nSession does not exist.");
    return;
}else{
    if(session.apiKey == null){
        Logger.error("Not Handling Message.\nUser is not Logged In.");
        return;
    }
}

/**
 * IF ACTIVITY IS IN CLOSED STATE STOP PRICE TIMER
 * */

if(session.activityInClosedState){
    new Thread(new Runnable() {
        @Override
        public void run() {
            Logger.info("Stopping Price Timer");
            session.stopTimer();
        }
    }).start();
}

/** ALWAYS WORKS IN BACKGROUND THREAD*/
try{
    PushNotification pushNotification = new PushNotification(data);
    updateNotificationBar(pushNotification);
    session.onNewAlert();
}catch (Exception ex){
    Logger.error("Exception: "+ex);
}
<?xml version="1.0" encoding="utf-8"?>
<uses-feature android:glEsVersion="0x00020000" android:required="false"/>

<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="com.google.android.c2dm.permission.RECEIVE" />

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

<application
    android:allowBackup="true"
    android:name=".objects.Session"
    android:icon="@mipmap/appicon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:hardwareAccelerated="true"
    >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustPan"
        android:screenOrientation="portrait"
        android:launchMode="singleTop"
    >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <!--GCM REGISTRATION STARTS -->
    <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:exported="true"
        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.sentieo.gcm" />
        </intent-filter>
    </receiver>

    <receiver android:name=".pushnotifications.GCMListenerService$NotificationCancelledBroadCastReceiver"
        android:exported="false"
        >
    </receiver>

    <service android:name=".pushnotifications.RegistrationIntentService" android:enabled="true"></service>
    <service
        android:name=".pushnotifications.GCMListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
    <service
        android:name=".pushnotifications.InstanceIDListenerService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.iid.InstanceID"/>
        </intent-filter>
    </service>
    <!--GCM REGISTRATION ENDS -->
</application>

共有1个答案

许展鹏
2023-03-14

尝试将这些权限放在清单文件的receiver标记中:

<receiver android:name=".MyReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
</receiver>

还要将此权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
 类似资料:
  • 我有一个应用程序,可以通过谷歌云消息接收消息。我需要在收到云消息时自动启动一个活动,必要时唤醒设备。 虽然它似乎是一个糟糕的UI设计,其中一些东西被有力地显示给用户,但这个应用程序对用户的工作是至关重要的,所以当一个云消息到达时,能够清楚地查看它对用户来说是更有用的。 当收到云消息时,我尝试调用,当应用程序位于后台时,它可以正常工作。但是,一旦该应用程序从最近的应用程序列表中删除,新活动就不会启动

  • 无论在应用程序的前台还是后台状态下,都没有在应用程序中得到任何消息,注册id正确,在IDE中检查。GCM响应为“成功”:2,“失败”:0

  • 如果可以的话,请帮我一下。我一直试图实现一个基本的gcm设置。在服务器端,我使用了上面答案中的php代码——GCM和PHP (Google Cloud Messaging ),在客户端,我遵循了Google的GCM教程。整个事情在我的Nexus 4上运行良好,但在我试过的两款4.0.4之前的设备上就不行了。一个是运行4.0.3的htc one v,一个是运行2.3.4的索尼爱立信。这可能是关于4.

  • 我可能已经阅读了所有其他与此相关的SO线程,但我找不到答案,而且在Android监视器中也没有日志跟踪。有人对此有提示吗?

  • 我对FireBase云消息有一个问题,我从设备中获取令牌,并通过Google FireBase通知控制台发送通知测试。但是,通知从来没有被记录,也没有被推送到android虚拟设备。FCM的文档几乎与下面的代码完全相同,其他代码很少,您还需要做什么来获得使用FireBase的推送通知。我已经完成了所有的设置信息(build.gradle添加、安装google play服务等)如文档中所指定的,但仍

  • 我们有一个制作人 在开发过程中,我重新部署了producer应用程序,并做了一些更改。但在此之后,我的消费者没有收到任何消息。我尝试重新启动消费者,但没有成功。问题可能是什么和/或如何解决? 消费者配置: 生产者配置: 编辑2: 5分钟后,消费者应用程序死亡,但以下情况除外: