使用Pushsharp&无法获得推送通知Pushplugin

时间:2017-02-07 20:06:09

标签: cordova asp.net-web-api phonegap-plugins phonegap-pushplugin pushsharp

我在我的Cordova应用程序中使用pushplugin - 前端。对于后端,我使用带有Pushsharp通知的webapi。

我可以向Pushsharp发送消息,但我无法收到通知。请建议。我正在使用Pushplugin生成注册令牌。

前端的pushplugin

document.addEventListener("deviceready", function () {
    pushNotification = window.plugins.pushNotification;
});
function InitNotification() {
    $("#app-status-ul").append('<li>registering ' + device.platform + '</li>');
    if (device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos") {
        pushNotification.register(
            successHandler,
            errorHandler,
            {
                "senderID": "XXXXXXXXXXXXXXX",
                "ecb": "onNotification"
            });
    }
    else {
        pushNotification.register(
            tokenHandler,
            errorHandler,
            {
                "badge": "true",
                "sound": "true",
                "alert": "true",
                "ecb": "onNotificationAPN"
            });
    }}

function successHandler(result) {
    console.log(result);
    alert('result = ' + result);
}

function errorHandler(error) {
    alert('error = ' + error);
}

function onNotification(e) {
    $("#app-status-ul").append('<li>EVENT -> RECEIVED:' + e.event + '</li>');
    switch (e.event) {
        case 'registered':
            if (e.regid.length > 0) {
                $("#app-status-ul").append('<li>REGISTERED -> REGID:' + e.regid + "</li>");
                // Your GCM push server needs to know the regID before it can push to this device
                // here is where you might want to send it the regID for later use.
                alert("regID = " + e.regid);
                var deviceOS = (deviceOs).toLowerCase();
                regID = e.regid;

                registerToken(clientID, regID, deviceOS, deviceId);

            }
            break;

        case 'message':
            // if this flag is set, this notification happened while we were in the foreground.
            // you might want to play a sound to get the user's attention, throw up a dialog, etc.
            if (e.foreground) {
                $("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');

                // on Android soundname is outside the payload.
                // On Amazon FireOS all custom attributes are contained within payload
                var soundfile = e.soundname || e.payload.sound;
                // if the notification contains a soundname, play it.
                var my_media = new Media("/android_asset/www/" + soundfile);
                my_media.play();
            }
            else {  // otherwise we were launched because the user touched a notification in the notification tray.
                if (e.coldstart) {
                    $("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
                }
                else {
                    $("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
                }
            }


            $("#app-status-ul").append('<li>MESSAGE -> MSG3: ' + e.payload.message + '</li>');

            //Only works for GCM
            $("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>');
            ////Only works on Amazon Fire OS
            //$status.append('<li>MESSAGE -> TIME: ' + e.payload.timeStamp + '</li>');
            break;

        case 'error':
            $("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
            break;

        default:
            $("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
            break;
    }     
}

webapi中的Pushsharp 4.0

[HttpGet]
//[Route("all/{m}/{direct:int:max(1)}")]
public IHttpActionResult All(string m, int direct = 1)
{
    bool isDirect = direct == 1;
    string responseMessage = String.Empty;
    var messagetitle = "PushSharpNotification";
    var APIKey = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
    var senderId = "XXXXXXXXXXXXX";
    var message = m;
    try
    {
        if (String.IsNullOrEmpty(m) || m.Length <= 2 || m.Length > 60)
            throw new Exception("The message must be between 3 and 60 characters!");
        var UserTokenID = DatabaseContext.MobileDevice.Select(x => x.PushNotificationsRegistrationID).ToList();

        var config = new GcmConfiguration(senderId,APIKey, null);
        config.GcmUrl = "https://fcm.googleapis.com/fcm/send"; //!!!!!gmc changed to fcm;)

        var gcmBroker = new GcmServiceBroker(config);
        gcmBroker.Start();
        foreach (var regId in UserTokenID)
        {
            // Queue a notification to send
            gcmBroker.QueueNotification(new GcmNotification
            {
                RegistrationIds = new List<string> { regId },
                Notification = JObject.Parse(
                    "{" +
                    "\"title\" : \"" + messagetitle + "\"," +
                    "\"body\" : \"" + message + "\"," +
                    "}"),
            });
        }

        // Stop the broker, wait for it to finish   
        // This isn't done after every message, but after you're
        // done with the broker
        gcmBroker.Stop();

        // Stop the broker, wait for it to finish   
        // This isn't done after every message, but after you're
        // done with the broker
        //  gcmBroker.Stop();
        return Ok(m);
    }
    catch (Exception ex)
    {
        return Ok(ex.Message);
    }
}

0 个答案:

没有答案