作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Android 我使用的是 phonegap 1.7(cordova-1.7.0.js)。我正在使用 Urban Airship 发送推送通知。
当我收到消息时,“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);
});
最佳答案
查看我的 1.5+ 或更高版本插件迁移指南:
http://simonmacdonald.blogspot.com/2012/04/migrating-your-phonegap-plugins-to.html
此外,您是否记得将插件行放在 plugins.xml 中?
关于android - phonegap 1.7 notificationCallback(此函数 this.sendJavascript(js))不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10949884/
这是我的代码。当我的应用程序被终止并在推送通知上重新启动时,它应该正确重定向,但它实际上从未进入 pushNotification.notificationCallback = function(ev
Android 我使用的是 phonegap 1.7(cordova-1.7.0.js)。我正在使用 Urban Airship 发送推送通知。 当我收到消息时,“notificationCallba
我是一名优秀的程序员,十分优秀!