【问题标题】:titanium android notification must open specific window钛 android 通知必须打开特定窗口
【发布时间】:2014-12-18 05:03:49
【问题描述】:

我正在使用 Titanium appcelerator(使用合金)创建一个 Android 应用程序,我创建了一个状态栏通知,问题是当通知出现时,我想打开一个特定窗口并在用户点击通知时向其传递一些数据,所以我的代码是:

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

当用户点击通知时,确实,应用程序来到了前面,但在离开的同一个窗口中,而不是想要的窗口。

我快到了,给我一点推力。

谢谢。

【问题讨论】:

    标签: android titanium titanium-alloy


    【解决方案1】:

    好的,我来回答我自己的问题,我浪费了整整 2 天的时间来做这个,我开始认为没有一个大的钛开发社区,无论如何这里有一些要点:

    1. 没有与android通知相关的Alloy示例 状态栏,所有的例子都是经典的钛应用,所以我 不得不猜测应该将我的活动 js 文件放在哪里。
    2. 特别注意意图标志,我搞砸了。我用了 FLAG_ACTIVITY_RESET_TASK_IF_NEEDED,根据android official documentation,听起来不错,但在钛金属中则不行 工作。我避免在这种情况下使用操作,这个 Intent 只有flagsurl 属性。
    3. 同样的方式,待处理的意图应该只有activityflags 属性。

    在 Titanium 3.4.0.GA 中工作的代码是:

    // 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中声明activity很重要,这个配置必须在android节点里面

    <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 文件必须在

    应用 > 资产 > 安卓

    当用户点击通知时,该文件中的代码将被执行。**

    【讨论】:

    • 如果你使用的是alloy,你也可以将你的activity js文件保存在lib文件夹中。只需在 app 目录中创建一个 lib 文件夹即可。
    猜你喜欢
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多