AlarmManager与PhoneGap的结合

6
我正在使用Phonegap的StatusBarNotification插件(Android)来触发通知。现在我想在特定时间执行此操作,根据我所读的内容,我必须使用Android的AlarmManager。我尝试了一些方法,但似乎无法使其正常工作。
有任何建议吗?
编辑: 如果我将代码放在onReceive()中的showNotification()中,我就可以显示通知。 问题似乎是接收器没有接收到Alarm-thingy。可能是因为我的IntentFilter中没有正确的操作。
这是我的代码。我从Phonegap的StatusBarNotification插件构建它,在这里找到
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);
        }
    }
}

}

2个回答

1

你应该使用LocalNotification.js来设置特定日期和时间的提醒。因为我已经使用过它,而且它运行良好。


以下是他们在 GitHub 项目页面上的一些文档和示例:https://github.com/phonegap/phonegap-plugins/tree/master/Android/LocalNotification - Nikolai Samteladze

0

那是我尝试过的事情之一。可能没有做对。现在我有一个“问题”,就是我应该将什么样的操作添加到我的IntentFilter中呢?这是当我查看BroadcastReceiver示例时遇到的。 - user713821
我自己也正打算为一个应用解决这个问题。如果我找到了解决方案,我会关注这个问题并进行更新... - Devgeeks
是的,互联网发展很快。四年时间很长。几乎所有这些信息都已经过时了,即使有用的地方,链接也显然已经过期。Apache Cordova不再被称为“回调”(谢天谢地),旧的PhoneGap插件仓库已被取代,而且现在也已经过时了。:) 简而言之:避免在SO上找四年前的答案。 - Devgeeks

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接