对于Android 我正在使用phonegap 1.7(cordova-1.7.0.js)。我正在使用城市飞艇发送推送通知。
当我收到消息时,“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 :(得分:0)
请查看我的1.5+或更高版本的插件迁移指南:
http://simonmacdonald.blogspot.com/2012/04/migrating-your-phonegap-plugins-to.html
另外,你还记得把你的插件行放在plugins.xml中吗?