2

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);
});
4

1 に答える 1

0

1.5 以降のプラグインの移行ガイドをご覧ください。

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

また、plugins.xml に plugins 行を入れたことを覚えていますか?

于 2012-06-08T14:43:48.377 に答える