2012-02-01 2 views
6

Phonegap에 StatusBarNotification 플러그인 (Android)을 사용하여 알림을 발생시킵니다. 이제 특정 시간에이 작업을하고 싶습니다. 그리고 지금까지 읽은 것부터 Android의 AlarmManager를 사용해야합니다. 몇 가지 접근법을 시도했지만 작동시키지 못합니다.Phonegap이있는 AlarmManager

어떻게 할 수 있습니까?

EDIT : onReceive() 코드를 showNotification()에 넣으면 알림 메시지가 표시됩니다. 문제는 수신자가 Alarm-thingy를 수신하지 않는 것으로 보입니다. 아마도 IntentFilter에 올바른 작업이 없기 때문일 것입니다.

이것은 내 코드입니다. 내가 폰갭에 대한 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); 
     } 
    } 
} 

} 당신은 아마도 알람 관리기 물건을 할 수있는 폰갭 플러그인을 작성해야합니다

답변

0
+0

내가 시도한 것들 중 하나입니다. 아마 그걸 제대로하지 않았을거야. 내가 지금 "문제"를 가지고있는 한 가지는, IntentFilter에 어떤 종류의 행동을 추가해야 하는가입니다. 이것은 BroadcastReceiver 예제를 살펴볼 때입니다. – user713821

+0

저는 실제로 앱에 대해이 문제를 직접 해결하려고합니다. 해결책을 찾으면이 질문과 업데이트에 계속 눈을 뗄 것입니다 ... – Devgeeks

+0

링크가 작동하지 않습니다. 404 오류. –

1

당신은 특정 날짜와 시간에 대한 경고를 설정하는 LocalNotification.js를 사용해야합니다. 내가 그것을 사용했기 때문에 그것은 잘 작동합니다.

+0

몇 가지 샘플 코드를 추가 할 수 있습니까? –

+0

다음은 github 프로젝트 페이지의 일부 문서 및 예입니다. https://github.com/phonegap/phonegap-plugins/tree/master/Android/LocalNotification –