GCM在设备上收到消息但在重新启动电话后未发送到应用程序

时间:2015-07-28 12:22:05

标签: android push-notification google-cloud-messaging

我已经在android上添加了GCM通知服务,但是当设备重新启动时我没有看到任何传递给应用的消息。

我检查了打印的日志,它显示了一个日志说:

"I/GCM﹕ GCM message com.package.name 0:1438085xxxxxxxxxxxxxxxxxx" 

这意味着设备已收到该消息,但该消息未转发给该应用。有什么建议吗?

以下是我对onMessageReceived的实施方式:

public void onMessageReceived(String from,Bundle data){

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 个答案:

答案 0 :(得分:0)

尝试将这些权限放入清单文件的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"/>