钛Android通知必须打开特定的窗口 [英] titanium android notification must open specific window

查看:138
本文介绍了钛Android通知必须打开特定的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Appcelerator的钛(采用合金)创建一个Android应用程序,我创建了一个状态栏通知,问题是当通知出现,我想打开一个特定的窗口,并通过它的一些数据,当用户点击该通知,所以我的code是:

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.

感谢。

推荐答案

好吧,这里我去回答我的问题,我浪费了2天全面执行此,我开始觉得,没有钛的大社区开发者,反正有些importants点这里:

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. 有没有一种合金例如涉及到Android的通知
    状态栏,所有的例子都是经典的钛应用程序,所以我
    猜应该在哪里把我的活动js文件。

  2. 意图的标志特别注意,我搞砸了这一点。我用了
    FLAG_ACTIVITY_RESET_TASK_IF_NEEDED ,与根据<一个href=\"http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_RESET_TASK_IF_NEEDED\"相对=nofollow>安卓官方
    文档,它可以正确的声音,但在钛它不
    工作。我避免这种情况下使用的动作,这个意图只有标记
    网​​址属性。

  3. 同样的方法,待定的意图应该只有活动标记
    属性。

  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.

在code钛3.4.0.GA工作将是:

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

重要声明中tiapp.xml活动中,这个配置必须是Android的节点内

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>

**重要,如果你使用的是合金的活动js文件必须在

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

应用程序>资产>机器人

app > assets > android

这是当用户点击该文件中的code将要执行的通知在那里。**

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

这篇关于钛Android通知必须打开特定的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆