我正在使用 Phonegap 的 StatusBarNotification 插件 (Android) 来触发通知。现在我想在特定时间进行此操作,根据我所读的内容,我必须使用 Android 的 AlarmManager。我已经尝试了一些方法,但似乎无法让它发挥作用。
关于我如何做到这一点有什么建议吗?
编辑: 如果我将代码放在 onReceive() 到 showNotification() 中,我可以获得显示的通知。 问题似乎是接收器没有收到警报之类的东西。可能是因为我在 IntentFilter 中没有正确的操作。
这是我的代码。我从 Phonegap 的 StatusBarNotification 插件构建它,发现 here
public class StatusBarNotification extends Plugin {
// Action to execute
public static final String ACTION="notify";
private Context context;
BroadcastReceiver receiver;
public StatusBarNotification() {
this.receiver = null;
}
public void setContext(PhonegapActivity ctx) {
super.setContext(ctx);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(); //Dunno what to put here
if(receiver == null) {
this.receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Notification", "inside onReceive");
/*int icon = R.drawable.notification;
long when = System.currentTimeMillis();
NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, context.getClass());
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification noti = new Notification(icon, "NOTIFICATION", when);
noti.setLatestEventInfo(context, "TITLE", "TEXT", contentIntent);
manager.notify(1, noti);
*/
}
};
ctx.registerReceiver(this.receiver, intentFilter);
}
}
/**
* Executes the request and returns PluginResult
*
* @param action Action to execute
* @param data JSONArray of arguments to the plugin
* @param callbackId The callback id used when calling back into JavaScript
*
* @return A PluginRequest object with a status
* */
@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
String ns = Context.NOTIFICATION_SERVICE;
context = this.ctx.getApplicationContext();
PluginResult result = null;
if (ACTION.equals(action)) {
try {
String title = data.getString(0);
String body = data.getString(1);
Log.d("NotificationPlugin", "Notification: " + title + ", " + body);
showNotification(title, body);
result = new PluginResult(Status.OK);
} catch (JSONException jsonEx) {
Log.d("NotificationPlugin", "Got JSON Exception "
+ jsonEx.getMessage());
result = new PluginResult(Status.JSON_EXCEPTION);
}
} else {
result = new PluginResult(Status.INVALID_ACTION);
Log.d("NotificationPlugin", "Invalid action : "+action+" passed");
}
return result;
}
/**
* Displays status bar notification
*
* @param contentTitle Notification title
* @param contentText Notification text
* */
public void showNotification( CharSequence contentTitle, CharSequence contentText) {
Intent intent = new Intent(ctx, ctx.getClass());
PendingIntent pi = PendingIntent.getBroadcast(ctx, 1234, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
AlarmManager am = (AlarmManager) ctx.getSystemService(ctx.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
}
public void onDestroy() {
if (this.receiver != null) {
try {
this.ctx.unregisterReceiver(this.receiver);
} catch (Exception e) {
Log.e("LOG TAG", "Error unregistering network receiver: " + e.getMessage(), e);
}
}
}
最佳答案
您应该使用 LocalNotification.js 为特定日期和时间设置警报。因为我已经使用过它并且效果很好。
关于android - 带 phonegap 的 AlarmManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9104144/