简体   繁体   中英

Titanium Android Notification perform action when user clicks notification

I haven't succeed in responding notification in Titanium Alloy. All working examples are classic ones. Although I have defined following activity in tiampl.xml

As you see even though tiapp.xml contains notification.js activity AndroidManifest.xml does not contain this activity. It should add this!

<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>

I have defined notification.js in lib folder and my index.js file is:

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>

I should see in AndroidManifest.xml following missing expected 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>

Nothing happens when I click notification. But following classic example works. classic example code that works

The right way to add activities via tiapp.xml is to add them under the <android> element and not at the root-level. See http://docs.appcelerator.com/platform/latest/#!/guide/tiapp.xml_and_timodule.xml_Reference-section-29004921_tiapp.xmlandtimodule.xmlReference-Android-specificsection

I figured out you have use <activities> outside of <activity> with url otherwise it does not work also if use <application> if you want to use <application> use custom 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM