Android GCM 启动特定活动失败



我有以下代码从GCM服务内部生成通知:

private void sendNotification(Bundle extras) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    String notification_type = extras.getString("type");
    NotificationManager notificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, extras.getString("message"), when);
    String title = this.getString(R.string.app_name);

    Intent notificationIntent = new Intent(this, InvitationActivity.class);
    if ( notification_type != null && notification_type.equals("invitation"))
    {
        notificationIntent = new Intent(this, InvitationActivity.class);
        notificationIntent.putExtra("lobby_id", Integer.valueOf(extras.getString("messagedata", "0")));
    } else {
        notificationIntent = new Intent(this, MainActivity.class);
    }

    // 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(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(this, title, extras.getString("message"), intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;
    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);
}

问题是如果我单击通知,主活动就会启动,但是如果

notificationIntent = new Intent(this, InvitationActivity.class);

被调用,我点击通知,似乎什么都没有发生,没有错误,没有启动任何活动。这只发生在邀请活动,主活动工作正常。

邀请活动:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_invitation);

    lobby_id = getIntent().getIntExtra("lobby_id", 0);

编辑:AndroidManifest.xml:

 <activity
        android:name=".activities_fragments.MainActivity"
        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=".activities_fragments.InvitationActivity"
        android:label="@string/title_activity_invitation"
        android:theme="@android:style/Theme.Holo.Dialog">
    </activity>

奇怪的是,如果我从MainActivity中启动InvitationActivity,它会按预期工作,但不能从GCMService内部工作。

添加

android:exported="true"

在您的邀请活动标签中,如下所示:

<activity
    android:name=".activities_fragments.InvitationActivity"
    android:label="@string/title_activity_invitation"
    android:exported="true"
    android:theme="@android:style/Theme.Holo.Dialog">
</activity>

这将解决您的问题

最新更新