我的应用程序关闭后,无法接收GCM消息

3

我希望当我的应用被关闭或从缓存中清除时,能够向用户展示GCM通知。以下是我的代码,它只在应用程序处于打开状态时才能工作,但不能在后台运行:

public function send_notification($registatoin_ids, $message) {
    include_once 'dbconfig.php';
    // Set POST variables
    $url = 'https://android.googleapis.com/gcm/send';
    $fields = array(
        'registration_ids' => $registatoin_ids,
        'data' => $message,
    );

智能手机无法在我的应用中看到任何服务。下面是一个截图,您可以将我的应用与其他应用如WhatsApp进行比较:enter image description here

以下是关于GCM的我的清单部分:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.app.path" >    

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
 <!-- ... other permissions -->

<permission
    android:name="my.app.path.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="my.app.path.permission.C2D_MESSAGE" />

<application
    ...>
     <!-- ... activites... -->

    <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" />
            <category android:name="my.app.path" />
        </intent-filter>
    </receiver>      

    <service
        android:name=".MyGcmListener"
        android:exported="false"
        android:enabled="true" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>

</application>

</manifest>

我想展示类似WhatsApp的通知。


2
  1. 迁移到FCM
  2. 阅读 https://dev59.com/U1oU5IYBdhLWcg3wCz2X#37876727
- Tim
是否没有 FCM 是不可能的?我对 FCM 一无所知。 - amit gupta
检查所有类型的用户权限和使用此链接 http://stackoverflow.com/questions/38422551/gcm-push-notification-not-revived/38423390#38423390 的权限。 - Hardik Parmar
1个回答

0
你尝试过将消息优先级设置为吗?
将优先级设置为高优先级可使GCM服务在可能的情况下唤醒睡眠设备并打开到您的应用程序服务器的网络连接。例如,具有即时消息、聊天或语音通话警报的应用程序通常需要打开网络连接并确保GCM立即将消息传递给设备。仅在消息时间至关重要且需要用户立即交互时才设置为高优先级,并注意将消息设置为高优先级会比普通优先级消息更加耗电。

1
我尝试了这个解决方案来在发送消息时设置高优先级选项,但它并没有解决我的问题。 - amit gupta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接