1

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

应用程序.js

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

我的服务.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 点创建通知。任何可能的解决方法?

4

1 回答 1

1

您将不得不使用 Bencoding AlarmManagerhttps ://github.com/benbahrenburg/benCoding.AlarmManager

这个模块提供了你需要的东西。这真的很容易 - 只需在调度通知或服务时设置repeat“每日” 。

有关完整功能的代码,请参阅https://gist.github.com/itsamiths/6248106

于 2013-08-15T08:32:40.860 回答