Search code examples
titaniumandroid-notifications

Titanium - App does show from notification only once


My app sometimes displays multiple android notifications. When someone clicks on them, my app should show up with the content of the notifications. I've managed to do it, but it only works for the first notification the user clicks on. If I leave the application after this (for example with the homebutton) and click then on the next notification, nothing happens.

I've created an example application to show you what I have at the moment. You can find it here: https://github.com/VanCoding/TitaniumNotificationTest

What am I doing wrong? How do I achieve what I want?

app.js

if(!Ti.App.Properties.getBool("displayedNotifications")){
    var notifications = ["Apple","Orange","Banana"];

    for(var i = 0; i < notifications.length; i++){
        var intent = Ti.Android.createIntent({
            action: Ti.Android.ACTION_MAIN,
            packageName:"com.company.notificationtest",
            className:"com.company.notificationtest.NotificationtestActivity",
            flags:Ti.Android.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED  | Ti.Android.FLAG_ACTIVITY_SINGLE_TOP
        });
        intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);
        intent.putExtra("name",notifications[i]);

        Titanium.Android.NotificationManager.notify(i, Titanium.Android.createNotification({
            contentTitle: notifications[i],
            contentText : notifications[i],
            contentIntent: Ti.Android.createPendingIntent({
                intent:intent,
                type : Ti.Android.PENDING_INTENT_FOR_ACTIVITY,
                flags : Ti.Android.FLAG_ACTIVITY_NO_HISTORY
            }),
            flags : Titanium.Android.ACTION_DEFAULT | Titanium.Android.FLAG_AUTO_CANCEL | Titanium.Android.FLAG_SHOW_LIGHTS
        }));
    }

    Ti.App.Properties.setBool("displayedNotifications",true);
}

tiapp.xml

<?xml version="1.0" encoding="UTF-8"?>
<ti:app xmlns:ti="http://ti.appcelerator.org">
    <id>com.company.notificationtest</id>
    <name>NotificationTest</name>
    <version>1.0</version>
    <publisher>not specified</publisher>
    <url></url>
    <description>not specified</description>
    <copyright>not specified</copyright>
    <icon>appicon.png</icon>
    <fullscreen>false</fullscreen>
    <navbar-hidden>false</navbar-hidden>
    <analytics>true</analytics>
    <guid>91e86075-373b-44e0-9416-66183390e8af</guid>
    <property name="ti.ui.defaultunit" type="string">dp</property>
    <android xmlns:android="http://schemas.android.com/apk/res/android">
    </android>

    <modules>
    </modules>
    <deployment-targets>
        <target device="android">true</target>
    </deployment-targets>
    <sdk-version>3.2.3.GA</sdk-version>
</ti:app>

Solution

  • Changing:

    contentIntent: Ti.Android.createPendingIntent({
                    intent:intent,
                    type : Ti.Android.PENDING_INTENT_FOR_ACTIVITY,
                    flags : Ti.Android.FLAG_ACTIVITY_NO_HISTORY
                }),
    

    to:

    contentIntent: Ti.Android.createPendingIntent({
                    intent:intent,
                    type : Ti.Android.PENDING_INTENT_FOR_ACTIVITY
                }),
    

    fixes it for me. Tested using 3.3.0.GA.