gpt4 book ai didi

android - GCM 推送通知延迟

转载 作者:行者123 更新时间:2023-11-29 15:43:31 24 4
gpt4 key购买 nike

我真的卡在这里了,当我发出推送通知时,消息延迟了大约 10 分钟。我检查了 logcat,只有当 packageName 未锁定时,消息才会直接到达。我该如何控制它?

非常感谢任何帮助。

这是我的日志

逻辑猫

com.xxx.xxx D/MyGcmListenerService: From: 12345678
com.xxx.xxx D/MyGcmListenerService: Message: New Alert: #02 (ABC123)
com.xxx.xxx V/ContextImpl: ----- packageName = com.xxx.xxx is NOT LOCKED -----

list

<!-- [START gcm_permission] -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- [END gcm_permission] -->

<!-- [START gcm_receiver] -->
<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="com.xxx.xxx" />
</intent-filter>
</receiver>
<!-- [END gcm_receiver] -->


<!-- [START instanceId_listener] -->
<service
android:name=".notification.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
<!-- [END instanceId_listener] -->



<!-- [START gcm_listener] -->
<service
android:name=".notification.MyGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<!-- [END gcm_listener] -->



<service
android:name=".notification.RegistrationIntentService"
android:exported="false">
</service>

服务器端

pushNotification("xxxxxxx", "#02(hello)");

function pushNotification($registatoin_ids, $message) {

// prepare variables for push notification
$message = array("message" => "$message", "time_to_live" => 10000, "collapse_key" => "sample", "delay_while_idle" => true);
$registatoin_ids = array("$registatoin_ids");

// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';

$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);

$GOOGLE_API_KEY = 'XXXXXXXXXX';
$headers = array(
'Authorization: key=XXXXXXXXXX',
'Content-Type: application/json'
);
//print_r($headers);
// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}

// Close connection
curl_close($ch);

// echo $result;
}

MyGcmListenerService

    public class MyGcmListenerService extends GcmListenerService {

private static final String TAG = "MyGcmListenerService";
private static final String MyPREFERENCES = "xxxx";
private static final String JOB_KEY = "xxxx";

private String JobStatus;

int countNotification = 0;


@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);

sendNotification(message);
}


private void sendNotification(String message) {

SharedPreferences sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
JobStatus = sharedPreferences.getString(JOB_KEY, "");


if (message.contains("#02")) {

String submsg = message.substring(message.indexOf("(") + 1, message.indexOf(")"));

Intent launch = new Intent(Intent.ACTION_MAIN);
launch.setClass(getApplicationContext(), AnnouncementActivity.class);
launch.addCategory(Intent.CATEGORY_LAUNCHER);
launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
launch.putExtra("message", submsg);
startActivity(launch);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(0);
}
}
}

最佳答案

尝试在 PHP 代码中的数组 $fields 上添加 'priority' => 'high'

$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
'priority' => 'high'
);

https://developers.google.com/cloud-messaging/concept-options#setting-the-priority-of-a-message

High priority. GCM attempts to deliver high priority messages immediately, allowing the GCM service to wake a sleeping device when possible and open a network connection to your app server. Apps with instant messaging, chat, or voice call alerts, for example, generally need to open a network connection and make sure GCM delivers the message to the device without delay. Set high priority only if the message is time-critical and requires the user’s immediate interaction, and beware that setting your messages to high priority contributes more to battery drain compared to normal priority messages.

关于android - GCM 推送通知延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36613692/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com