通过我的应用程序中的gcm.jar在应用程序中集成推送通知,该工作正常工作直到8小时后。我知道那是我收到最后一次的时间。
这是我的GCM意向服务:
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super(CommonUtilities.SENDER_ID);
}
@Override
protected void onRegistered(Context context, String registrationId) {
Log.i(TAG, "Device registered: regId = " + registrationId);
CommonUtilities.displayMessage(context, getString(R.string.gcm_registered));
ServerUtilities.register(context, registrationId);
}
@Override
protected void onUnregistered(Context context, String registrationId) {
Log.i(TAG, "Device unregistered");
CommonUtilities.displayMessage(context, getString(R.string.gcm_unregistered));
if (GCMRegistrar.isRegisteredOnServer(context)) {
ServerUtilities.unregister(context, registrationId);
} else {
// This callback results from the call to unregister made on
// ServerUtilities when the registration to the server failed.
Log.i(TAG, "Ignoring unregister callback");
}
}
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = intent.getStringExtra("message");
String url = intent.getStringExtra("url");
CommonUtilities.displayMessage(context, message);
// notifies user
generateNotification(context, message, url);
}
@Override
protected void onDeletedMessages(Context context, int total) {
Log.i(TAG, "Received deleted messages notification");
String message = getString(R.string.gcm_deleted, total);
CommonUtilities.displayMessage(context, message);
// notifies user
generateNotification(context, message,"");
}
@Override
public void onError(Context context, String errorId) {
Log.i(TAG, "Received error: " + errorId);
CommonUtilities.displayMessage(context, getString(R.string.gcm_error, errorId));
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
// log message
Log.i(TAG, "Received recoverable error: " + errorId);
CommonUtilities.displayMessage(context, getString(R.string.gcm_recoverable_error,
errorId));
return super.onRecoverableError(context, errorId);
}
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message, String url) {
int icon = R.drawable.app_icon;
long when = System.currentTimeMillis();
Log.e(TAG, message);
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent;
if(!TextUtils.isEmpty(url)){
notificationIntent= new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse(url));
}else{
notificationIntent= new Intent(context, SplashScreen.class);
notificationIntent.putExtra("message", message);
}
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
}
使用适当的权限,在清单中:
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.packageappname" />
</intent-filter>
</receiver>
已经提到过(实际上已经实际实施):http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/
现在我没有收到正在推送的通知。我查了一下:http://developer.android.com/google/gcm/index.html
我是否必须强制实施新的GCM才能使其正常运行,或者我的意向服务是否存在问题?
答案 0 :(得分:0)
我在我的一个旧应用程序中使用C2DM,并且只是测试推送,它在这里工作。 有时在wifi家用路由器或任何rauter之后推送不通过,也许它只是我的路由器但无论如何重启路由器。迁移并不是一个坏主意,我这样做了,花了我一个星期的时间来学习和获取正确的东西。你看到了migrate page
吗?