我正在尝试使用gcm通知。我的服务器代码工作正常,我得到了成功作为承认。
问题是通知正常发送:
1)在大多数设备中,即时收到通知。 在Google Nexus,sony手机上测试。 $ b 2)其他设备也会在几个小时后收到通知。是,小时。测试Karbonn,Micromax的一些手机。注意: 所有设备连接到相同的WiFi,所以网络连接不成问题。在服务器端使用PHP; 关于此主题有几个未解答的问题。我特此列出其中的一些:
gcm通知不适用于micromax等设备
/ questions / 26132407 / one-device-doesnt-receive-push-notifications-gcm>一个设备没有收到推送通知(GCM)推送通知延迟与GCM
任何其他有类似问题的人都会附上您的问题。
更正尝试失败: $ b
我也经历了几个问题,其中开发人员发现他们的解决方案如从 onHandleIntent()
GcmBroadcastReceiverpleteWakefulIntent(intent);或将 delay_while_ideal 值更改为<$ c
或者单独提及接收者和注册意图 - 过滤器
$在服务器代码中为$ c> true / false b $ b < intent-filter> < category android:name =com.nothing.gcmclient/> < / intent-filter> < intent-filter> < category android:name =com.nothing.gcmclient/> < / intent-filter>代码: Android.manifest
<?xml version =1.0encoding =utf-8?> < manifest xmlns:android =schemas.android/apk/res/android package =com.nothing.gcmclient android:versionCode = 1 android:versionName =1.0> < uses-sdk android:minSdkVersion =9 android:targetSdkVersion =22/> <使用权限android:name =android.permission.INTERNET/> <使用权限android:name =android.permission.GET_ACCOUNTS/> <使用权限android:name =android.permission.WAKE_LOCK/> < permission android:name =com.nothing.gcmclient.permission.C2D_MESSAGE android:protectionLevel =signature/> <使用权限android:name =com.nothing.gcmclient.permission.C2D_MESSAGE/> < uses-permission android:name =com.nothing.gcmclient.c2dm.permission.RECEIVE/> < uses-permission android:name =android.permission.ACCESS_NETWORK_STATE/> < uses-permission android:name =android.permission.VIBRATE/> < uses-permission android:name =ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE>< / uses-permission> <使用权限android:name =android.permission.READ_PHONE_STATE/> <使用权限android:name =android.permission.READ_EXTERNAL_STORAGE/> < application android:icon =@ drawable / ic_launcher android:label =@ string / app_name> < activity android:name =。RegisterActivity android:label =@ string / app_name> < intent-filter> < category android:name =android.intent.category.LAUNCHER/> < / intent-filter> < / activity> < activity android:name =。MainActivity android:configChanges =orientation | keyboardHidden android:label =@ string / app_name> < / activity> < receiver android:name =。GcmBroadcastReceiver android:permission =com.google.android.c2dm.permission.SEND> < intent-filter> < category android:name =com.nothing.gcmclient/> < / intent-filter> < intent-filter> < category android:name =com.nothing.gcmclient/> < / intent-filter> < / receiver> < service android:name =。GCMNotificationIntentService>< / service> < activity android:name =。ChatActivity android:label =@ string / title_activity_chat> < / activity> android:name =。RegisterScreen android:label =@ string / title_activity_register_screen> < / activity> < activity android:name =。RegisterChatButtonActivity android:label =@ string / title_activity_register_chat_button> < / activity> < activity android:name =。ChatHistory android:label =@ string / title_activity_chat_history> < / activity> < activity android:name =。MessageScreen android:label =@ string / title_activity_message_screen> < / activity> < / application> < / manifest>代码:GCMNotificationIntentService.java
public class GCMNotificationIntentService extends IntentService { public static final int NOTIFICATION_ID = 1; private NotificationManager mNotificationManager; NotificationCompat.Builder构建器; public GCMNotificationIntentService(){ super(GcmIntentService); } public static final String TAG =GCMNotificationIntentService; @Override protected void onHandleIntent(Intent intent){ Bundle extras = intent.getExtras(); GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 字符串messageType = gcm.getMessageType(intent); $ b $ if(!extras.isEmpty()){ if(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR .equals(messageType)){ sendNotification(Send error:+ extras.toString()); } else if(GoogleCloudMessaging.MESSAGE_TYPE_DELETED .equals(messageType)){ sendNotification(服务器上的已删除邮件: + extras.toString()); } else if(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE .equals(messageType)){ String sender = extras.get(Config.SENDER).toString()。toLowerCase(); String message = extras.get(Config.MESSAGE_KEY).toString(); $ b $ if(!RegisterActivity.appVisible == true) { sendNotification(New message Received from+ extras.get(Config.SENDER)); } } } //GcmBroadcastReceiverpleteWakefulIntent(intent); private void sendNotification(String msg){ Log.d(TAG,准备发送通知...:+ msg); mNotificationManager =(NotificationManager)this .getSystemService(Context.NOTIFICATION_SERVICE); PendingIntent contentIntent = PendingIntent.getActivity(this,0, new Intent(this,ChatHistory.class),0); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( this).setSmallIcon(R.drawable.gcm_cloud) .setContentTitle(New Notification)。 setStyle(new NotificationCompat.BigTextStyle()。bigText(msg)) .setContentText(msg); mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(NOTIFICATION_ID,mBuilder.build()); Log.d(TAG,通知发送成功。); $ b $ p $ b请专家仔细查看问题并提出相应的原因和解决方案。如果您的应用程序仅适用于某些手机,我的应用程序将无法使用。如果您需要在我们的应用程序中提供更多文件,请及时通知。
PS-请仔细阅读整个问题并然后发布您的评论或答案或必要时标记重复。
如何解决设备无法连接时的常见问题在Google Play服务上使用wifi或经常失去连接:
请参阅这篇文章产品论坛获取更多信息。
I am trying to use gcm notification. My server code is working fine and I am getting success as acknowledgment.
The problem is notification is getting sent properly :
1) In most of devices notification is received instantaneously. Tested on google nexus,sony phones.
2) Other devices are also receiving notification but after several hours. Yes, hours. Tested on Karbonn, Micromax some phones.
Note:
All the devices are connected to the same wifi so network connectivity not an issue . Using php at server side; There are several unanswered questions regarding this topic . I am hereby listing some of them:
gcm notification is not working on some devices like micromax
One device doesn't receive push notifications (GCM)
Push notifications delay with GCM
Anyone else having similar problem attach your question too.
Failed trials of rectification:
I have also made changes to code after going through several questions in which the developers found their solution like removing this line of code from onHandleIntent()
GcmBroadcastReceiverpleteWakefulIntent(intent);Or changing delay_while_ideal value to true/false in server code.
Or separately mentioning receiver and registration intent-filter
<intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.nothing.gcmclient" /> </intent-filter> <intent-filter> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="com.nothing.gcmclient" /> </intent-filter>Code: Android.manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="schemas.android/apk/res/android" package="com.nothing.gcmclient" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="22" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <permission android:name="com.nothing.gcmclient.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.nothing.gcmclient.permission.C2D_MESSAGE" /> <uses-permission android:name="com.nothing.gcmclient.c2dm.permission.RECEIVE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".RegisterActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity" android:configChanges="orientation|keyboardHidden" android:label="@string/app_name" > </activity> <receiver android:name=".GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.nothing.gcmclient" /> </intent-filter> <intent-filter> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="com.nothing.gcmclient" /> </intent-filter> </receiver> <service android:name=".GCMNotificationIntentService"></service> <activity android:name=".ChatActivity" android:label="@string/title_activity_chat" > </activity> <activity android:name=".RegisterScreen" android:label="@string/title_activity_register_screen" > </activity> <activity android:name=".RegisterChatButtonActivity" android:label="@string/title_activity_register_chat_button" > </activity> <activity android:name=".ChatHistory" android:label="@string/title_activity_chat_history" > </activity> <activity android:name=".MessageScreen" android:label="@string/title_activity_message_screen" > </activity> </application> </manifest>Code: GCMNotificationIntentService.java
public class GCMNotificationIntentService extends IntentService { public static final int NOTIFICATION_ID = 1; private NotificationManager mNotificationManager; NotificationCompat.Builder builder; public GCMNotificationIntentService() { super("GcmIntentService"); } public static final String TAG = "GCMNotificationIntentService"; @Override protected void onHandleIntent(Intent intent) { Bundle extras = intent.getExtras(); GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); String messageType = gcm.getMessageType(intent); if (!extras.isEmpty()) { if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR .equals(messageType)) { sendNotification("Send error: " + extras.toString()); } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED .equals(messageType)) { sendNotification("Deleted messages on server: " + extras.toString()); } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE .equals(messageType)) { String sender=extras.get(Config.SENDER).toString().toLowerCase(); String message=extras.get(Config.MESSAGE_KEY).toString(); if(!RegisterActivity.appVisible==true) { sendNotification("New message Received from "+ extras.get(Config.SENDER)); } } } //GcmBroadcastReceiverpleteWakefulIntent(intent); } private void sendNotification(String msg) { Log.d(TAG, "Preparing to send notification...: " + msg); mNotificationManager = (NotificationManager) this .getSystemService(Context.NOTIFICATION_SERVICE); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ChatHistory.class), 0); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( this).setSmallIcon(R.drawable.gcm_cloud) .setContentTitle("New Notification") .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) .setContentText(msg); mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); Log.d(TAG, "Notification sent successfully."); } }Experts please look into the problem and suggest the appropriate cause and solution. My app would be of no use if it works only for certain phones.If you need any more file in our application kindly inform.
PS- Kindly read the entire question and then post your comments or answers or mark duplicate if necessary.
解决方案- GCM works through Google Play Services
- Devices connect to Google Play Services through TCP on port 5228
- Devices should use port 443 as fallback if port 5228 is blocked, but apparently they just don't use the fallback sometimes (happened to multiple of my devices)
- Device sends a heartbeat packet to Google Play Services every 28 minutes on mobile and 15 minutes on wifi
- You can check the connection state, heartbeat interval, connection address and port, etc. dialing *#*#426#*#* on a device
How to fix common issues when devices don't connect to Google Play Services on wifi or lose connection frequently:
See this post on Google Product Forums for more information.
更多推荐
某些设备在几小时后收到Gcm通知
发布评论