Question

I am creating a native android app using Titanium Appcelerator which shows notification daily at some specified time (say 8.00 a.m).In app.js the android service is called.

app.js

var intent = Ti.Android.createServiceIntent({
url : 'MyService.js'
});
intent.putExtra('interval', new Date().getTime());
Ti.Android.startService(intent);

MyService.js

var activity = Ti.Android.currentActivity();
var intent = Ti.Android.createIntent({
action : Ti.Android.ACTION_MAIN,
url : 'app.js',
flags : Ti.Android.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Ti.Android.FLAG_ACTIVITY_SINGLE_TOP
});
intent.addCategory(Titanium.Android.CATEGORY_LAUNCHER);
var pending = Ti.Android.createPendingIntent({
activity : activity,
intent : intent,
type : Ti.Android.PENDING_INTENT_FOR_ACTIVITY,
flags : Ti.Android.FLAG_ACTIVITY_NO_HISTORY
});
var notification = Ti.Android.createNotification({
contentIntent : pending,
contentTitle : 'Main Notification',
contentText : 'Whats Today',
tickerText : 'This is main notification',
when : "08:00",
icon : Ti.App.Android.R.drawable.appicon,
flags : Titanium.Android.ACTION_DEFAULT | Titanium.Android.FLAG_AUTO_CANCEL | Titanium.Android.FLAG_SHOW_LIGHTS
});
Ti.Android.NotificationManager.notify(1, notification);
var service = Ti.Android.currentService;
var serviceIntent = service.getIntent();

and my tiapp.xml is

<android xmlns:android="http://schemas.android.com/apk/res/android">
   <!-- the activities tag must be added if you want to use the url property to launch your app -->
   <activities>
       <activity url="app.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>
   <!-- the services tag must be added so that our service will run -->
   <services>
       <service type="interval" url="MyService.js"/>
   </services>
</android>

But this creates notification when application starts and I am unable to create notification daily at 8.00 am. Any possible workarounds?

Était-ce utile?

La solution

You will have to use Bencoding AlarmManager: https://github.com/benbahrenburg/benCoding.AlarmManager

This module provides what you need. It's really easy - just set repeat to 'daily' when sheduling a Notification or Service.

Refer https://gist.github.com/itsamiths/6248106 for fully functional code

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top