简体   繁体   中英

titanium android notification must open specific window

I'm creating an Android app using Titanium appcelerator(using alloy), I create a status bar notification, the issue is when the notification appears, I want open a specific window and pass it some data when the user taps the notification, so my code is:

// Intent object to launch the application
var intent = Ti.Android.createIntent({
    action : Ti.Android.ACTION_MAIN,
    flags : Ti.Android.FLAG_ACTIVITY_CLEAR_TOP | Ti.Android.FLAG_ACTIVITY_NEW_TASK,
    url : "window.js"
});
intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);
intent.putExtra("id", "10");
var activity = Ti.Android.currentActivity;
// Create a PendingIntent to tie together the Activity and Intent
var pending = Titanium.Android.createPendingIntent({
    activity : activity,
    intent : intent,
    type : Ti.Android.PENDING_INTENT_FOR_ACTIVITY,
    flags : Titanium.Android.FLAG_CANCEL_CURRENT
});

// Create the notification
var notification = Titanium.Android.createNotification({
    // icon is passed as an Android resource ID -- see Ti.App.Android.R.
    icon : Ti.App.Android.R.drawable.appicon,
    contentTitle : 'Something Happened',
    contentText : 'Click to return to the application.',
    contentIntent : pending,
    flags : Ti.Android.ACTION_DEFAULT | Ti.Android.FLAG_AUTO_CANCEL | Ti.Android.FLAG_SHOW_LIGHTS
});
// Send the notification.
Titanium.Android.NotificationManager.notify(1, notification);

when the user taps the notification, indeed, the app comes to front, but in the same window where was left, not exactly the wanted window.

I'm almost there, give me a little push.

Thanks.

Ok, here I go answering my own question, I wasted 2 full days performing this, I'm starting to think that there is no a big community of titanium devs, anyway some importants points here:

  1. There is no an Alloy example related to android notification status bar, all the examples are classic titanium application, so I had to guess where should put my activity js file.
  2. Special attention with intent flags, I screwed up with that. I used FLAG_ACTIVITY_RESET_TASK_IF_NEEDED , according with android official documentation , it could sound properly, but in titanium it does not work. I avoid use action for this case, this Intent only has flags and url attributes.
  3. The same way, pending intent should only have activity and flags attributes.

The code working in Titanium 3.4.0.GA would be:

// Intent object to launch the application
var intent = Ti.Android.createIntent({
    flags :  Ti.Android.FLAG_ACTIVITY_NEW_TASK | Ti.Android.FLAG_ACTIVITY_SINGLE_TOP,
    url: "videocomplete.js"
});
intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);
intent.putExtra("id", "10");
var activity = Ti.Android.currentActivity;
// Create a PendingIntent to tie together the Activity and Intent
var pending = Titanium.Android.createPendingIntent({
    intent : intent,
    flags : Titanium.Android.FLAG_CANCEL_CURRENT
});

// Create the notification
var notification = Titanium.Android.createNotification({
    // icon is passed as an Android resource ID -- see Ti.App.Android.R.
    icon : Ti.App.Android.R.drawable.appicon,
    contentTitle : 'Something Happened',
    contentText : 'Click to return to the application.',
    contentIntent : pending,
    flags : Ti.Android.ACTION_DEFAULT | Ti.Android.FLAG_AUTO_CANCEL | Ti.Android.FLAG_SHOW_LIGHTS
});
// Send the notification.
Titanium.Android.NotificationManager.notify(1, notification);

Important declaring the activity in tiapp.xml, this config must be inside of android node

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

**Important, if you are using alloy your activity js file has to be in

app > assets > android

from there when the user taps the notification the code in that file will be executed.**

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