php - 当我的应用程序关闭时,GCM 消息不会出现在我的应用程序中

标签 php android json google-cloud-messaging android-5.0-lollipop

我想在我的应用程序关闭或从缓存内存中清除时向用户显示我的 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 的 Manifest 部分:

<?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 这样的通知。

最佳答案

您是否尝试过将优先级设置为 high 是否有帮助?发送消息时?

设置为 High priority 允许 GCM 服务在可能的情况下唤醒 sleep 设备并打开与您的应用程序服务器的网络连接。例如,具有即时消息、聊天或语音调用提醒的应用程序通常需要打开网络连接并确保 GCM 将消息立即传送到设备。仅当消息时间紧迫且需要用户立即交互时才设置高优先级,并且请注意,与普通优先级消息相比,将消息设置为高优先级会更多地消耗电池电量。

关于php - 当我的应用程序关闭时,GCM 消息不会出现在我的应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38303887/

相关文章:

c# - Newtonsoft.Json - 反序列化大对象时出现内存不足异常

java - Gson:应为 begin_array 但为 STRING

php - 从数据库获取图片路径并显示在前端(Octobercms)

PHP FileToUpload 未定义索引错误

php - Laravel:表单提交时不显示错误消息

Android/ZXing 不再有效

android - TaskStackBuilder - 是否向后兼容?

android - Flutter:如何使用 RaisingButton 在 PageView 中导航或使用按钮移动 pageViewController

java - 使用 PHP 和 MySQL 的登录和注册显示 html 标签作为响应

asp.net - 使用 RenderPartialToString 时显示模型状态错误的问题