0

我刚刚开始使用 Android 开发。我按照以下指南在 Eclipse 中创建了一个 App Engine 连接的 Android 项目:创建一个 App Engine 连接的 Android 项目

该应用程序可以工作,但是当任务进入后台然后通过接收 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);
    }

    [...]
}

提前致谢!

4

3 回答 3

0

问题几乎解决了......不得不添加:

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

只剩下一个问题:当 GCM 消息将活动置于最前面时,仍然没有触发 onNewIntent()。我将代码放在 onCreate() 中。

于 2013-05-11T14:13:15.910 回答
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>
于 2013-05-10T07:38:18.143 回答
0

如果活动在后台,则接收意图,onNewIntent在 onResume 之前将调用该意图,因此您必须覆盖 onNewIntent

@Override
protected void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);

    boolean b = intent.getBooleanExtra("gcmIntentServiceMessage", false);
    ........  
}
于 2013-05-11T05:50:53.800 回答