Titanium Android 通知在用户单击通知时执行操作

Titanium Android Notification perform action when user clicks notification

我在 Titanium Alloy 中未能成功响应通知。 所有工作示例都是经典示例。虽然我在 tiampl.xml

中定义了以下 activity

如您所见,即使 tiapp.xml 包含 notification.js activity AndroidManifest.xml 也不包含此 activity。应该加上这个!

<id>alloyandroidnotification.example.com</id>
<name>alloyandroidnotification</name>
<activity url="notification.js"> 
<intent-filter> 
<action android:name="android.intent.action.VIEW"/> 
<category android:name="android.intent.category.DEFAULT"/> 
</intent-filter> 
</activity>

我在 lib 文件夹中定义了 notification.js,我的 index.js 文件是:

function doClick(e) {
        var intent = Ti.Android.createIntent({
            action: Ti.Android.ACTION_VIEW,
            packageName:"alloyandroidnotification.example.com",
            className:"alloyandroidnotification.example.com.NotificationActivity"
        });
        intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);


        Titanium.Android.NotificationManager.notify(1, Titanium.Android.createNotification({
            contentTitle: "notification",
            contentText : "notification",
            contentIntent: Ti.Android.createPendingIntent({
                intent:intent,
                type : Ti.Android.PENDING_INTENT_FOR_ACTIVITY
            }),
            flags : Titanium.Android.FLAG_AUTO_CANCEL | Titanium.Android.FLAG_SHOW_LIGHTS
        }));
}

$.index.open();

AndroidManifest.xml

<application android:icon="@drawable/appicon" android:label="alloyandroidnotification" android:name="AlloyandroidnotificationApplication" android:debuggable="false" android:theme="@style/Theme.AppCompat">
    <activity android:name=".AlloyandroidnotificationActivity" android:label="@string/app_name" android:theme="@style/Theme.Titanium" android:configChanges="keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity android:name="org.appcelerator.titanium.TiActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
    <activity android:name="org.appcelerator.titanium.TiTranslucentActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:theme="@style/Theme.AppCompat.Translucent"/>
    <activity android:name="ti.modules.titanium.ui.android.TiPreferencesActivity" android:configChanges="screenSize"/>
    <service android:name="com.appcelerator.analytics.APSAnalyticsService" android:exported="false"/>
</application>

我应该在 AndroidManifest.xml 中看到缺少预期 activity

  <android 
    xmlns:android="http://schemas.android.com/apk/res/android">

      <activity url="notificationClick.js">
        <intent-filter>
          <action android:name="android.intent.action.VIEW"/>
          <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
      </activity>


  </android>

单击通知时没有任何反应。但是以下经典示例有效。 classic example code that works

通过 tiapp.xml 添加活动的正确方法是将它们添加到 <android> 元素下,而不是在根级别。参见 http://docs.appcelerator.com/platform/latest/#!/guide/tiapp.xml_and_timodule.xml_Reference-section-29004921_tiapp.xmlandtimodule.xmlReference-Android-specificsection

我发现你在 <activity> 之外使用 <activities> 和 url 否则如果你想使用 <application> 它也不起作用 <application> =] 使用自定义 AndroidManifest.xml

<android xmlns:android="http://schemas.android.com/apk/res/android">
 <activities>
   <activity url="notificationClick.js">
     <intent-filter>
      <action android:name="android.intent.action.VIEW"/>
      <category android:name="android.intent.category.DEFAULT"/>
     </intent-filter>
   </activity>
</activities>
</android>