【问题标题】:titanium appcelerator android daily notification at some specified time钛appcelerator android每日通知在某个指定时间
【发布时间】:2013-08-16 13:18:03
【问题描述】:

我正在使用 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 点创建通知。任何可能的解决方法?

【问题讨论】:

    标签: titanium appcelerator titanium-mobile appcelerator-mobile


    【解决方案1】:

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

    这个模块提供你需要的东西。这真的很简单 - 只需在调度通知或服务时将 repeat 设置为 'daily'。

    完整功能代码请参考https://gist.github.com/itsamiths/6248106

    【讨论】:

    • 我试过了。但它说在创建 addAlarmService 之前,您必须为该警报定义 android 服务。我不知道如何为编码警报管理器创建一个 android 服务。我已经推荐了bencoding.com/2012/06/06/android-alarmmanager-for-titanium,但失败了。如果你知道如何给 android 服务名称请帮助
    • 在您的要点的 index.js 中,您如何将服务名称指定为 service:'com.manumaticx.notificationtest.BackgroundNotificationService' 从哪里获得服务名称?
    • 正如要点中所回答的,您可以从 /build/android/ 目录中钛生成的 AndroidManifest.xml 中获得完整的服务名称
    • 是的,我尝试完全重建..现在它可以工作了..我已经分叉了你的要点并进行了一些更改..签出..gist.github.com/itsamiths/6248106
    • 设备重启后是否可以重启bencoding报警服务?
    猜你喜欢
    • 2012-04-05
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多