Reputation: 1050
I really stuck at here, when I make a push notification, the message was delay around 10 minutes. I check on logcat, only when the packageName is NOT LOCKED, the message will arrived directly. How I can control this ?
Any help is really appreciated.
Here is my logcat
Logcat
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 -----
Manifest
<!-- [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>
ServerSide
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);
}
}
}
Upvotes: 1
Views: 4078
Reputation: 1622
There is a known issue that produces exactly these symptoms, thus is could be the cause of the behaviour you are seeing.
On cheaper wifi routers, the connection gets reset, so under these circumstances GCM only receives updates at the heartbeat interval.
TL;DR: try on a different wifi router, and see if you still have the same problem.
For more information see the following link:
https://eladnava.com/google-cloud-messaging-extremely-unreliable/
<quote / paste from above link> The problem with these intervals is caused by network routers and mobile carriers, who disconnect idle socket connections after a few minutes of inactivity. Usually, this is more common with cheap home routers, whose manufacturers decided on a maximum lifespan of an idle socket connection, and terminate it to save resources. These routers can only handle a finite number of concurrent connections, and so this measure is taken to prevent overload. This results in terminated GCM sockets,
Upvotes: 0
Reputation: 865
It's serverside issue
You can ask the server side person to change one of its property to delay_while_idle=0
This will solve your issue
Upvotes: 0
Reputation: 348
try to add 'priority' => 'high'
on your array $fields in php code
$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.
Upvotes: 3