Asif February 2016

Time based notifications

I am building an app which needs to send time-based notifications to the user.

I am using Notification Manager module and background services and pushing the notifications. This is working fine as long as the app is opened either in background or accessible.

Once I close the app, then notifications are not coming. How can I ensure that my service is running even though my app is closed and keep sending notifications?

Answers


cuppaBunny February 2016

Start your service using Titanium.Android.startService For Example,

app.js

var intent = Titanium.Android.createServiceIntent({
    url: 'myservice.js'
});
intent.putExtra('interval', 10000); // Service should run its code every 2 seconds.
intent.putExtra(Titanium.Android.EXTRA_DONT_KILL_APP, true);
Titanium.Android.startService(intent);

myservice.js

var intent = Ti.Android.createIntent({
   action: Ti.Android.ACTION_MAIN,
   className: 'com.testApp.TestappActivity',
   packageName: 'com.testApp'
});

intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);

var pending = Titanium.Android.createPendingIntent({
   intent: intent,
   flags: Titanium.Android.FLAG_UPDATE_CURRENT
});

var notification = Titanium.Android.createNotification({
   contentTitle: 'Something Happened',
   contentText: 'Click to return to the application.',
   contentIntent: pending
});

Titanium.Android.NotificationManager.notify(1, notification);

Also add service to your tiapp.xml,

<android xmlns:android="http://schemas.android.com/apk/res/android">
    <services>
        <service url="myservice.js" type="interval" />
    </services>
</android>

Post Status

Asked in February 2016
Viewed 1,123 times
Voted 11
Answered 1 times

Search




Leave an answer