从后台激活应用程序后 Android 意图出现问题



我刚刚开始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:nn" + 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);
    }
    [...]
}

提前感谢!

// 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:nn" + 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>

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

@Override
protected void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);
    boolean b = intent.getBooleanExtra("gcmIntentServiceMessage", false);
    ........  
}

问题几乎解决了...不得不补充:

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

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

最新更新