gpt4 book ai didi

android - phonegap 1.7 notificationCallback(此函数 this.sendJavascript(js))不起作用

转载 作者:行者123 更新时间:2023-11-29 13:58:18 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com