titanium appcelerator android在指定时间每日通知



我正在使用Titanium Appcelerator创建一个本地android应用程序,它每天在指定时间(比如早上8点)显示通知。在app.js中,会调用android服务。

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

并且我的tiapp.xml是

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

但这会在应用程序启动时创建通知,而我无法在每天早上8点创建通知。有什么可能的解决方法吗

您必须使用Bencoding AlarmManager:https://github.com/benbahrenburg/benCoding.AlarmManager

本模块提供您需要的内容。这真的很简单——在发送通知或服务时,只需将repeat设置为"每日"

请参阅https://gist.github.com/itsamiths/6248106对于全功能代码

最新更新