I am working on the Azure mobile app and try to push notification to ios, my app can push notification for android but can not for ios. because in the case ios I cannot register the device to notification hub in the client side, I am facing the issue
Here is my client code (ionic 2)
initPushNotification(userId = undefined, deviceToken = undefined, installationId = undefined) {
const options: PushOptions = {
android: {
senderID: 'mysender id',
forceShow: false,
sound: true
},
ios: {
alert: 'true',
badge: false,
sound: 'true'
},
windows: {}
};
this.platform.ready().then(() => {
const pushObject: PushObject = this.push.init(options);
pushObject.on('registration').subscribe((data: any) => {
let handle = data.registrationId;
let templateAndroid = {
body: { data: { message: "{$(message)}", notificationtype: "{$(notificationtype)}" } },
tags: [userId]
};
let templateiOS = {
body: { aps: { alert: "{$(message)}", notificationtype: "{$(notificationtype)}" } },
tags: [userId]
};
this.client = new WindowsAzure.MobileServiceClient("http://phattifymobileservice.azurewebsites.net");
if (!deviceToken || (deviceToken !== handle)) {
if (this.platform.is('ios')) {
this.typeDevice = 'ios';
this.client.push.register('apns', handle, { mytemplate: templateiOS }); -----> the error is here
} else if (this.platform.is('android')) {
this.typeDevice = 'android';
this.client.push.register('gcm', handle, { mytemplate: templateAndroid });
}
installationId && this.unregisterPush(deviceToken, installationId);
this.apiProvider.updateDeviceToken(handle, this.client.push.installationId).subscribe(res => {
console.log('update device token', res);
})
}
console.log('device token -> ' + data.registrationId);
});
let me = this;
pushObject.on('notification').subscribe(data => {
if (!data.additionalData.foreground) {
this.events.publish('push:on', { tabIndex: 1 });
}
})
pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
});
}
i tried many think for fix the issue cors domain, but it still isn't working here is my back end.
file app.js
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
res.setHeader('Access-Control-Allow-Headers', 'Origin, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, X-Requested-With, Date, X-Api-Version, X-Response-Time, X-PINGOTHER, X-CSRF-Token,Authorization, Access-Control-Request-Headers, If-Modified-Since, x-zumo-installation-id, x-zumo-version, zumo-api-version');
res.setHeader('Access-Control-Allow-Methods', 'GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS');
res.setHeader('Access-Control-Expose-Headers', 'X-Api-Version, X-Request-Id, X-Response-Time');
res.setHeader('Access-Control-Max-Age', '1000');
res.setHeader('Access-Control-Allow-Credentials', 'true');
next();
});
file push notification
var payload = {
message: JSON.parse(msg.Content).Content,
notificationType: config.get("notificationType")
};
context.push.send(Id, payload, function (error) {
if (!error) {
//notification sent
console.log(error);
}
});
Push notification for Android -----> working well
Push notification for ios-----> error
I don't know why I got this error, please help me out of this error
after tried some test, there are clues
My app is using the socket for the chat function so I need to enable the CORS. I tried to comment the CORS in code and log into Azure portal and set CORS under "API > CORS" of My mobile app then the Client IOS can register to notification hub so i can push notification, but the socket raise up to me 2 errors
cannot use wildcard in access-control-allow-origin when credentials flag is true
failed to load resource: cannot use wildcard in access-control-allow-origin when credentials flag is true so
Update
mobileApp.tables.initialize()
.then(function () {
app.use(mobileApp); // Register the Azure Mobile Apps middleware
var httpChat = require('http').Server(app);
var io = require('socket.io')(httpChat);
io.set('origins', 'http://localhost:8080');
require('./api/chat/service/Chat.js')(app, io, multer, uniqid, mkdirp);
require('./api/chat/service/Room.js')(app, io, multer, uniqid, mkdirp);
httpChat.listen(process.env.PORT || 3000); // Listen for requests
});

