从通知中调用活动会强制执行新任务,从而导致"空白"活动

时间:2014-06-19 23:11:04

标签: android notifications

我有一个可能会收到推送通知的应用。此推送通知应在用户单击时启动活动。非常标准。我让它在模拟器和我的Galaxy S4上运行,但是Nexus设备让我头痛。

起初我得到了:

java.lang.SecurityException: Permission Denial: start Intent 

关于该特定活动,我"已修复"通过插入android:exported =" true"进入清单xml中的那个Activity。不确定这是否是正确的方法,但我找不到更好的方法。

现在我要

​startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent 

活动开始,但这些额外变量似乎是" null":

opponentUsername = getIntent().getExtras().getString("opponentUsername");
gameId = getIntent().getExtras().getString("gameId");

导致"空"活性。

这是我在BroadcastReceiverClass中启动活动的方式:

Notification n = new Notification(icon, tickerText, when);

Intent intent = new Intent(ctx, SS9ChatActivity.class);
intent.putExtra("opponentUsername", dataObject.getString("oppo"));
intent.putExtra("gameId", dataObject.getString("game"));
PendingIntent pi = PendingIntent.getActivity(ctx, 0, intent, 0);
n.setLatestEventInfo(ctx, "Game", tickerText, pi);

n.flags |= Notification.FLAG_AUTO_CANCEL;                 
n.defaults |= Notification.DEFAULT_VIBRATE;
nm.notify(1, n);

我到目前为止尝试的是,在我的活动中插入android:launchMode="singleTop",但这似乎没有解决它

那我在这里做错了什么?为什么Nexus设备与仿真器和Galaxy S4有如此不同?我的应用程序运行正常。

提前感谢您的帮助!

这是我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.testapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />

        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />      

        <uses-permission android:name="android.permission.WAKE_LOCK" />
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
        <uses-permission android:name="android.permission.VIBRATE" />
        <uses-permission android:name="android.permission.GET_ACCOUNTS" />
        <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

        <permission android:protectionLevel="signature"
            android:name="com.testapp.permission.C2D_MESSAGE" />
        <uses-permission android:name="com.testapp.permission.C2D_MESSAGE" />       

    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/noAnimTheme">
        <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version"/>        
        <activity
            android:name="com.testapp.SplashScreenActivity"
            android:label="@string/app_name" 
            android:screenOrientation="sensorPortait"
            android:windowSoftInputMode="stateHidden"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

<activity android:name="MainActivity"
          android:label="@string/app_name" 
          android:screenOrientation="sensorPortait"
          android:windowSoftInputMode="stateHidden|adjustResize"/>          

<activity android:name="SS9ChatActivity"
          android:launchMode="singleTop"
          android:exported="true"
          android:label="@string/app_name" 
          android:screenOrientation="sensorPortait"
          android:windowSoftInputMode="stateHidden|adjustResize"/>

<activity android:name="com.facebook.LoginActivity"
          android:label="@string/app_name" />
        <meta-data android:name="com.facebook.sdk.ApplicationId" 
            android:value="@string/facebook_app_id"/>     

<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
  <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <action android:name="android.intent.action.USER_PRESENT" />
  </intent-filter>
</receiver>
<receiver android:name="com.parse.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.testapp" />
  </intent-filter>
</receiver>        
<receiver 
    android:name="com.testapp.PushBroadcastReceiver"
    android:exported="false"
    >
    <intent-filter>
      <action android:name="com.testapp.Push" />
    </intent-filter>
</receiver>         

 <activity android:name="com.google.android.gms.ads.AdActivity"
             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

singleTop是正确的答案。但是,您需要实施onNewIntent,因为传入的Intent未自动设置,因此您可以通过getIntent()检索其值:

protected void onNewIntent (Intent intent) {
  setIntent(intent); // now the rest of your code can use getIntent() as before
  handleIntent(); // this should also be called from onCreate as well
}