2012-09-06 60 views

回答

0

這是也接收在前景推送通知正在運行的應用的hackish代碼(用於有效載荷數據..)

(從xtify科爾多瓦樣品工作)

的@覆蓋公共無效之前添加此的onCreate :

private static MainActivity single; 
public static MainActivity get() { return single; } 

public MainActivity() { 
    super(); 
    single = this; 
} 

這有利於我們致電:

XtifyCordovaPlugin.processActivityExtras(msgExtras,MainActivity.get() );

在XtifyNotifier.java的onMessage功能

..並且因此推在前景接收,並傳送到科爾多瓦/ JavaScript的..

2

這是由設計,如果你想收到通知,只有當應用程序打開時,你需要在你的活動延伸DroidGap如下:

package com.xtify.cordova.samples; 

import android.content.Context; 
import android.content.IntentFilter; 
import android.os.Bundle; 

import com.xtify.cordova.XtifyCordovaPlugin; 
import com.xtify.sdk.api.XtifyBroadcastReceiver; 

public class MainActivity extends org.apache.cordova.DroidGap { 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     super.loadUrl("file:///android_asset/www/index.html"); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     this.registerReceiver(this.xtifyReceiver, new IntentFilter(
       "com.xtify.sdk.NOTIFIER")); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     this.unregisterReceiver(this.xtifyReceiver); 
    } 

    private XtifyBroadcastReceiver xtifyReceiver = new XtifyBroadcastReceiver() { 
     @Override 
     protected void onRegistered(Context arg0) { 
      // no-op 
     } 

     @Override 
     protected void onMessage(Context arg0, Bundle msgExtras) { 
      XtifyCordovaPlugin.processActivityExtras(msgExtras, 
        MainActivity.this); 
     } 

     @Override 
     protected void onC2dmError(Context arg0, String arg1) { 
      // no-op 
     } 
    }; 
} 

,你也需要修改com.xtify。 cordova.XtifyCordovaPlugin.processActivityExtras方法如下:

public static void processActivityExtras(Bundle msgExtras, DroidGap dg) { 
    if (msgExtras != null && (msgExtras.getString("com.xtify.sdk.NOTIFICATION_TITLE") != null)) { 
     try { 
      JSONObject data = new JSONObject(); 
      for (String key : msgExtras.keySet()) { 
       data.put(key, msgExtras.getString(key)); 
      } 
      Context context = dg.getApplicationContext().getApplicationContext(); 
      String eventCallBackName = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString(KEY_CALLBACK, ""); 
      String js = "javascript:" + eventCallBackName + "(" + data.toString() + ")"; 
      dg.sendJavascript(js); 
     } catch (JSONException e) { 
      Log.e(TAG, "Error processing activity extras", e); 
     } 
    } 
} 

另外,請確保您設置的通知行動NONE在Xtify合作或者在Push API中。

+0

感謝,雖然我想接收推送無論是在背景和前景(用於有效載荷有用數據 - 這對我們來說是許多後備之一..)。 最後我們黑了它看到我的答案.. –