简体   繁体   中英

Titanium Appcelerator unable to send push notification to a specified user

I'm making an app using appcelerator alloy framework which needs push notifications. I'm using push notifications for the first time, so bear with me and help me out here.

I've followed the push notification wiki tutorial here https://wiki.appcelerator.org/display/guides2/Push+Notifications

This is my code here :

var deviceToken = null;

// Check if the device is running iOS 8 or later
if (Ti.Platform.name == "iPhone OS" && parseInt(Ti.Platform.version.split(".")[0]) >= 8) {

    // Wait for user settings to be registered before registering for push notifications
    Ti.App.iOS.addEventListener('usernotificationsettings', function registerForPush() {

        // Remove event listener once registered for push notifications
        Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush); 

        Ti.Network.registerForPushNotifications({
            success: deviceTokenSuccess,
            error: deviceTokenError,
            callback: receivePush
        });
    });

    // Register notification types to use
    Ti.App.iOS.registerUserNotificationSettings({
        types: [
            Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT,
            Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND,
            Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE
        ]
    });
}

// For iOS 7 and earlier
else {

    Ti.Network.registerForPushNotifications({
        // Specifies which notifications to receive
        types: [
            Ti.Network.NOTIFICATION_TYPE_BADGE,
            Ti.Network.NOTIFICATION_TYPE_ALERT,
            Ti.Network.NOTIFICATION_TYPE_SOUND
        ],
        success: deviceTokenSuccess,
        error: deviceTokenError,
        callback: receivePush
    });
}

// Process incoming push notifications
function receivePush(e) {
    alert('Received push: ' + JSON.stringify(e));
}
// Save the device token for subsequent API calls
function deviceTokenSuccess(e) {
    deviceToken = e.deviceToken;
}

function deviceTokenError(e) {
    alert('Failed to register for push notifications! ' + e.error);
}


// Require the Cloud module
var Cloud = require("ti.cloud");

function subscribeToChannel () {
    // Subscribes the device to the 'chats' channel
    // Specify the push type as either 'android' for Android or 'ios' for iOS
    Cloud.PushNotifications.subscribeToken({
        device_token: deviceToken,
        channel:'test',
        type: Ti.Platform.name == 'android' ? 'android' : 'ios'
    }, function (e) {
        if (e.success) {
            alert('Subscribed');
        } else {
            alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
        }
    });
}



function unsubscribeToChannel () {
    // Unsubscribes the device from the 'test' channel
    Cloud.PushNotifications.unsubscribeToken({
        device_token: deviceToken,
        channel:'test',
    }, function (e) {
        if (e.success) {
            alert('Unsubscribed');
        } else {
            alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
        }
    });
}



function loginUser(username, password){
    // Log in to Arrow
    Cloud.Users.login({
        login: username,
        password: password
    }, function (e) {
        if (e.success) {
            subscribeToChannel ();
            alert('Login successful with device token' + deviceToken);

            // Store the authentication details in the local filesystem
            Ti.App.Properties.setString('usernameSave',username);
            Ti.App.Properties.setString('passwordSave',password);

            // user_id = jsonPost.SuccessResult.user_id;

        } else {
            alert('Error:\n' +
                ((e.error && e.message) || JSON.stringify(e)));
        }
    });
}



var savedUserName = Ti.App.Properties.getString('usernameSave','');
var savedPassword = Ti.App.Properties.getString('passwordSave','');
if(savedUserName != ''){
    $.userNameField.value = savedUserName;
    $.passwordField.value = savedPassword;
}

function login(){
    var username = $.userNameField.value;
    var password = $.passwordField.value;

    loginUser(username, password);
}

The Login() function is called when a button named login is clicked.

I get the Login Successful and Subscribed alerts as expected on login.

Whenever I tried sending a push notification to all the users, it worked. But if I try to send it to a specified user it gives me a failure on the Push Logs in the dashboard.

What am I missing here? Please help me out.

Thanks.

Ok I found the problem that was causing this.

Yeah it was my fault as in the subscription method I'm using token subscription instead of channel subscription. As I'm using the session based method.

Here is the difference, if anyone needs it in future.

Check the second line...

Previous Code

function subscribeToChannel () {

    Cloud.PushNotifications.subscribeToken({
        device_token: deviceToken,
        channel:'test',
        type: Ti.Platform.name == 'android' ? 'android' : 'ios'
    }, function (e) {
        if (e.success) {
            alert('Subscribed');
        } else {
            alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
        }
    });
}

New Code

function subscribeToChannel(){

    Cloud.PushNotifications.subscribe({
        device_token: deviceToken,
        channel: 'test',
        type: Ti.Platform.name == 'android' ? 'android' : 'ios'
    }, function (e) {
        if (e.success) {
            alert('Subscribed');
        } else {

            alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
        }
    });
}

Thank you.

Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM