2012-02-01 8 views
6

私は通知を発生させるためにPhoneGapにStatusBarNotificationプラグイン(Android)を使用しています。今私はこれを特定の時間にしたいと思います。私が読んだことから、AndroidのAlarmManagerを使用する必要があります。私はいくつかのアプローチを試しましたが、それを働かせるようには見えません。Phonegap付きAlarmManager

どうすればいいですか?

EDIT: onReceive()のコードをshowNotification()に挿入すると、通知が表示されます。 問題は、受信者がAlarm-thingyを受信して​​いないように見えます。おそらく私はIntentFilterに適切なアクションがないからでしょう。

これは私のコードです。私は、PhoneGapのためStatusBarNotificationプラグインからそれを構築しましたあなたはおそらくAlarmManagerのものを行うことができますPhoneGapのプラグインを記述する必要があります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); 
     } 
    } 
} 

}

答えて

0
+0

これは私が試したことの1つです。おそらくそれは正しいことではありませんでした。私が "問題"を抱えているのは、IntentFilterにどのようなアクションを加えなければならないのでしょうか?これは私がBroadcastReceiverの例を見ているときです。 – user713821

+0

私は実際にこれについて自分自身に取り組んでいます。解決策を見つけると、この質問と更新に目を通していきます... – Devgeeks

+0

リンクが機能していません404エラー。 –

1

あなたが特定の日付と時刻のアラートを設定するLocalNotification.jsを使用する必要があります。私はそれを使用していたので、うまく動作します。

+0

いくつかのサンプルコードを追加できますか? –

+0

githubプロジェクトページのドキュメントと例があります:https://github.com/phonegap/phonegap-plugins/tree/master/Android/LocalNotification –

関連する問題