0
votes

I need to implement PushNotification for iOS device.

The error message this when I want to subscribe a channel Missing fields. Required: device_token and channel

I have apple certificate and all configurations in Appcelerator dashboard.

And when I am running the app on my device app asking to confirm push notification.

I believe the error appears because of Ti.Network.registerForPushNotifications events not firing.

Here is my code.

var Cloud = require("ti.cloud");

login();

function login() {

env = Ti.App.deployType.toLowerCase() === 'production' ? 'production' : 'development',
username = Ti.App.Properties.getString('acs-username-'+env),
password = Ti.App.Properties.getString('acs-password-'+env);

// Places your already created user id credential
Cloud.Users.login({
    login : username,
    password : password
}, function(e) {
    if (e.success) {

        var user = e.users[0];
        Ti.API.info(' Login Success:\n' + 'id: ' + user.id + '\n' + 'sessionId: ' + Cloud.sessionId + '\n' + 'first name: ' + user.first_name + '\n' + 'last name: ' + user.last_name);

    } else {
        Ti.API.info('Error:\n' + ((e.error && e.message)));
    }
});

}

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() {

        Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush);

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

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

function receivePush(e) {
    //Ti.API.info('Received push: ' + JSON.stringify(e));
}

function deviceTokenSuccess(e) {
    deviceToken = e.deviceToken;
    Ti.API.info("eeeeeeee" + e);
    //subscribeToChannel();
}

function deviceTokenError(e) {
    Ti.API.info('ssssssssFailed to register for push notifications! ' + e.error);
}

function subscribeToChannel () {
    // Subscribes the device to the 'news_alerts' channel
    // Specify the push type as either 'android' for Android or 'ios' for iOS

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

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

setTimeout(function() {
    subscribeToChannel();
  }, 6000);
1
Are you sure you getting device token? And also make sure that you have created this channel: 'news_alerts' on Appc Dashboard before registering users. - Prashant Saini

1 Answers

0
votes

Before making calls to Arrow DB, make sure your deviceTokenSuccessCB is getting fired and since you have indicated that it's not getting fired then you can below steps for it: