2012-02-01 21 views
6

Tôi đang sử dụng plugin StatusBarNotification (Android) cho Phonegap để kích hoạt thông báo. Bây giờ tôi muốn điều này tại một thời điểm cụ thể, và từ những gì tôi đã đọc tôi phải sử dụng AlarmManager của Android. Tôi đã thử một số cách tiếp cận, nhưng dường như không làm cho nó hoạt động.AlarmManager với phonegap

Bất kỳ đề xuất nào về cách tôi có thể thực hiện việc này?

EDIT: Tôi có thể nhận thông báo để hiển thị nếu tôi đặt mã trong onReceive() để hiển thịThông báo(). Vấn đề có vẻ là người nhận không nhận được báo động-thingy. Có lẽ vì tôi không có hành động đúng trong IntentFilter.

đây là mã của tôi. Tôi đã xây dựng nó từ các plugin StatusBarNotification cho PhoneGap, tìm thấy 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); 
     } 
    } 
} 

}

Trả lời

0

Bạn có thể sẽ phải viết một plugin PhoneGap có thể làm những thứ AlarmManager.

Dưới đây là một số ví dụ về BroadcastReceivers trong plugin:

https://github.com/callback/callback-android/blob/master/framework/src/com/phonegap/NetworkManager.java

https://github.com/phonegap/phonegap-plugins/blob/master/Android/PhoneListener/PhoneListener.java

+0

Đó là một trong những điều tôi đã thử. Có lẽ đã không làm điều đó đúng. Một điều tôi gặp phải là "vấn đề" bây giờ, tôi nên thêm loại hành động nào vào IntentFilter của mình? Đây là khi tôi đang xem các ví dụ về BroadcastReceiver. – user713821

+0

Tôi thực sự sắp giải quyết vấn đề này cho một ứng dụng. Tôi sẽ theo dõi Câu hỏi này và cập nhật nếu tôi tìm thấy giải pháp ... – Devgeeks

+0

Liên kết không hoạt động 404 Lỗi. –

1

Bạn nên sử dụng LocalNotification.js để thiết lập cảnh báo cho ngày và thời gian cụ thể. Bởi vì tôi đã sử dụng nó và nó hoạt động tốt.

+0

bạn có thể thêm một số mã mẫu không? –

+0

Dưới đây là một số tài liệu và ví dụ từ trang dự án github của họ: https://github.com/phonegap/phonegap-plugins/tree/master/Android/LocalNotification –

Các vấn đề liên quan