我刚开始使用Android开发。我按照以下指南在Eclipse中创建了一个App Engine连接的Android项目:Creating an App Engine Connected Android Project。
应用程序可以运行,但是当任务进入后台然后通过接收GCM消息再次激活时,GCMIntentService类调用的意图不会到达相应的活动。可能是什么问题?
public class GCMIntentService extends GCMBaseIntentService {
[...]
@Override
public void onMessage(Context context, Intent intent) {
sendNotificationIntent(context, "Message received via Google Cloud Messaging:\n\n" + intent.getStringExtra("message"), true, false);
}
[...]
private void sendNotificationIntent(Context context, String message, boolean isError, boolean isRegistrationMessage) {
Intent notificationIntent = new Intent(context, RegisterActivity.class);
notificationIntent.putExtra("gcmIntentServiceMessage", true);
notificationIntent.putExtra("registrationMessage", isRegistrationMessage);
notificationIntent.putExtra("error", isError);
notificationIntent.putExtra("message", message);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(notificationIntent);
}
[...]
}
提前致谢!
答案 0 :(得分:0)
// add permission <uses-permission android:name="android.permission.WAKE_LOCK" />
@Override
public void onMessage(Context context, Intent intent) {
sendNotificationIntent(context, "Message received via Google Cloud Messaging:\n\n" + intent.getStringExtra("message"), true, false);
context.sendBroadcast(intent) ;
}
private void sendNotificationIntent(Context context, String message, boolean isError, boolean isRegistrationMessage) {
Intent notificationIntent = new Intent(context, RegisterActivity.class);
notificationIntent.putExtra("gcmIntentServiceMessage", true);
notificationIntent.putExtra("registrationMessage", isRegistrationMessage);
notificationIntent.putExtra("error", isError);
notificationIntent.putExtra("message", message);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = neNotification(R.drawable.ic_launcher,"Title",System.currentTimeMillis());
PendingIntent intents = PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
notification.setLatestEventInfo(context , context.getString(R.string.app_name), tickerText , intents );
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(100, notification);
}
public class AppBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction() == "AppReceiver") {
// this intent is gcminit intent and you can get data from this Intent
}
}
}
// in menifest file
<receiver android:name="AppBroadcastReceiver" >
<intent-filter>
<action android:name="AppReceiver" >
</action>
</intent-filter>
</receiver>
答案 1 :(得分:0)
如果活动在后台,则意图在onNewIntent
中接收,这将在onResume之前调用,因此您必须覆盖onNewIntent
@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
boolean b = intent.getBooleanExtra("gcmIntentServiceMessage", false);
........
}
答案 2 :(得分:0)
问题几乎解决了......不得不补充:
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
只剩下一个问题:当GCM消息将活动带到前面时,仍然不会触发onNewIntent()。我把代码放在onCreate()中。