Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Notifications to multiple devices

$
0
0

Hi I am working on an app that needs to send notifications to multiple devices at once.

I have all keys set up and devices subscribed to a test channel. I have implemented notification before but did not send to multiple devices.

Some code:

PushFunc.js

var Cloud = require('ti.cloud');
var badge_number;
var APIKEY = 'correcttkey';
 
exports.subscribeToChannel = function devcieToken(){
    Cloud.PushNotifications.subscribeToken({
        channel: 'test',
        type: Ti.Platform.name == 'android' ? 'android' : 'ios',
        device_token: deviceToken
    }, function (e) {
        if (e.success) {
            //alert('Success : subscribed to test channel');
        } else {
            //alert('Error:' + ((e.error && e.message) || JSON.stringify(e)));
        }
    });
};
 
 
exports.uploadReport = function(deviceToken, incidentTitle, NAME) {
    var deviceTokenString = "";
 
    for (i = 0; i < deviceToken.length; i++){
        if (i == 0){
            deviceTokenString = deviceToken[i].NotID;
        }
        else{
            deviceTokenString = deviceTokenString+","+deviceToken[i].NotID;
        }
    }
 
    console.log('deviceTokenString is '+deviceTokenString+'.');
 
    Cloud.PushNotifications.notifyTokens({
        to_tokens: deviceTokenString,
        channel: 'test',
        payload: {
            "message":"message using defined params",
            "alert":"message using defined params",
            "type":"uploadReport",
            "badge": "+1",
            "icon":"appicon"
        }
    }, function(e){
        if(e.success) {
            alert('Push notification sent');
        } else {
            console.log('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
        }
    });
};
I first tried passing in an array to the 'to_tokens' but that did not work, so i switched to a comma separated string.

I am getting into e.success and getting the alert Push notification sent. I am also receiving the notifications on the correct test devices here. However, the app then crashes after that.

I am sending the notification in an on success of another web service which posts info to a database (below).

if (webserviceResponse=="OK"){
    alert("Successfully posted");   
 
    //I create an array with ids returned from this web service and call it deviceTokenArray
    if (deviceTokenArray.length >0){
        var myPushFunc = require('ui/handheld/pushFunc');
        myPushFunc.uploadReport(deviceTokenArray, definedVariable, definedVariable);
    }
    else{
        alert('no matching tokens');
    }               
}
I'm at a loss for the reason of the crash...It is hardly a Titanium bug?

Any help appreciated.

Titanium Classic 3.4.1. Testing with iPhone6 (8.1) sending notifications and iPad and iPhone 4s receiving the notification. Most recent version of Ti.cloud


Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>