2012-06-08 110 views
2

For Android我使用的是phonegap 1.7(cordova-1.7.0.js)。我使用城市飞艇发送推送通知。phonegap 1.7 notificationCallback(this function this.sendJavascript(js))is not working

当我收到消息时,“notificationCallback”函数不起作用。这是我的代码。

 PushNotificationPlugin.java 

    package org.minimoesfuerzo.plugins; 

    import org.json.JSONArray; 
    import org.json.JSONException; 
    import org.json.JSONObject; 

    import android.util.Log; 

    import com.phonegap.api.Plugin; 
    import com.phonegap.api.PluginResult; 
    import org.apache.cordova.api.PluginResult.Status; 

    public class PushNotificationPlugin extends Plugin { 
     final static String TAG = PushNotificationPlugin.class.getSimpleName(); 

    static PushNotificationPlugin instance = null; 

    public static final String ACTION = "registerCallback"; 

    public PushNotificationPlugin() { 
     instance = this; 
    } 

    public static PushNotificationPlugin getInstance() { 
     return instance; 
    } 

    public void sendResultBack(String msg, String payload) { 
     JSONObject data = new JSONObject(); 
     try { 
      data.put("msg", msg); 
      data.put("payload", payload); 
     } catch (JSONException e) { 
      Log.e(TAG, e.getMessage()); 
     } 
     String js = String.format("window.plugins.pushNotification.notificationCallback('%s');", data.toString()); 
     this.sendJavascript(js); 
    } 

    @Override 
    public PluginResult execute(String action, JSONArray data, 
      String callbackId) { 

     PluginResult result = null; 
     if (ACTION.equals(action)) { 
      result = new PluginResult(Status.NO_RESULT); 
      result.setKeepCallback(false); 
     } else { 
      Log.d(TAG, "Invalid action: " + action + " passed"); 
      result = new PluginResult(Status.INVALID_ACTION); 
     } 
     return result; 
    } 
} 

接收机类

 package org.minimoesfuerzo.ua; 

    import org.minimoesfuerzo.plugins.PushNotificationPlugin; 

    import android.content.BroadcastReceiver; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.util.Log; 

    import com.appstockholm.zoud.MainActivity; 
    import com.urbanairship.UAirship; 
    import com.urbanairship.push.PushManager; 

    public class IntentReceiver extends BroadcastReceiver { 

    //private static final String TAG = IntentReceiver.class.getSimpleName(); 
    private static final String TAG = "humayoo"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.i(TAG, "Received intent: " + intent.toString()); 
     String action = intent.getAction(); 

     if (action.equals(PushManager.ACTION_PUSH_RECEIVED)) { 
      int id = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, 0); 

      Log.i(TAG, "Received push notification. Alert: " + intent.getStringExtra(PushManager.EXTRA_ALERT) 
       + ". Payload: " + intent.getStringExtra(PushManager.EXTRA_STRING_EXTRA) + ". NotificationID="+id); 

      String alert = intent.getStringExtra(PushManager.EXTRA_ALERT); 
      String extra = intent.getStringExtra(PushManager.EXTRA_STRING_EXTRA); 

      PushNotificationPlugin plugin = PushNotificationPlugin.getInstance(); 
      plugin.sendResultBack(alert, extra); 

     } else if (action.equals(PushManager.ACTION_NOTIFICATION_OPENED)) { 
      Log.i(TAG, "User clicked notification. Message: " + intent.getStringExtra(PushManager.EXTRA_ALERT) 
        + ". Payload: " + intent.getStringExtra(PushManager.EXTRA_STRING_EXTRA)); 

      Intent launch = new Intent(Intent.ACTION_MAIN); 
      launch.setClass(UAirship.shared().getApplicationContext(), MainActivity.class); 
      launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

      UAirship.shared().getApplicationContext().startActivity(launch); 
      String alert = intent.getStringExtra(PushManager.EXTRA_ALERT); 
      String extra = intent.getStringExtra(PushManager.EXTRA_STRING_EXTRA); 

      PushNotificationPlugin plugin = PushNotificationPlugin.getInstance(); 
      plugin.sendResultBack(alert, extra); 

     } else if (action.equals(PushManager.ACTION_REGISTRATION_FINISHED)) { 
      Log.i(TAG, "Registration complete. APID:" + intent.getStringExtra(PushManager.EXTRA_APID) 
        + ". Valid: " + intent.getBooleanExtra(PushManager.EXTRA_REGISTRATION_VALID, false)); 
     } 
    } 
} 

Pushnotification.js文件

var PushNotification = { 
    registerCallback: function(successCallback, failureCallback) { 
     return PhoneGap.exec(
       successCallback,   // called when signature capture is successful 
       failureCallback,   // called when signature capture encounters an error 
       'PushNotificationPlugin', // Tell PhoneGap that we want to run "PushNotificationPlugin" 
       'registerCallback',  // Tell the plugin the action we want to perform 
       []);      // List of arguments to the plugin 
    }, 
    notificationCallback: function(json) { 
     var data = window.JSON.parse(json); 
     alert("Success: " + data.msg); 

    } 
}; 

PhoneGap.addConstructor(function() { 
    PhoneGap.addPlugin("pushNotification", PushNotification); 
}); 
+0

只是好奇,如果你发现在此期间修复。我也有同样的问题。 – toto2

回答

0

看看我的迁移指南插件上1.5+或更高版本:

http://simonmacdonald.blogspot.com/2012/04/migrating-your-phonegap-plugins-to.html

另外,你是否记得把你的插件行放在plugins.xml中?

+0

1.i将我的插件放到plugin.xml文件中。 – Humayoo

+0

1.i将我的插件放到plugin.xml文件中。 2.when当我从城市飞艇接收器类调用发送回结果函数(“sendResultBack”提到pushnotificationplugin.java文件在上面的帖子中) ,“this.sendJavascript(js)”不起作用。它通过null 在你的文章中没有提到关于sendcallback函数的调用 – Humayoo