Open ickata opened 7 years ago
@ickata I am afraid that this is not a known issue.
Can you please supply examine the following:
If this does not help, can you please provide some examples of your setup and code to be able to replicate this locally.
Thanks in advance.
@AntonDobrev , thanks for advising me to follow the NS log. I was deploying app on device via XCode. After deploying app via tns run ios
I saw the JS error in the log. The problem was in my code, however I think that the cause requires attention. I am using both local and push notifications plugins. For my local notifications I always count on notification ID, because I take care to pass such. The error was in the local notification addOnMessageReceivedCallback
. I'm not sure that invoking local notification callback on receiving a push notification is good – in my mind this breaks encapsulation. Is this an issue?
Hi,
Any update here?
@ickata Thanks for specifying this. I am sorry that I wasn't able to attend to your question in a more timely manner. The remote push notifications plugin does not influence the behavior of the local notifications. Here I am suspecting that either a system event is called and the other plugin is subscribed to it (on first sight these seems the custom notificationReceived event).
Perhaps the error was due to missing notId raised in the local notifications callback, can you please specify more details? This may require more thorough examination but we will need more details on the use case and setup.
@AntonDobrev , no worries! I solved my issue and moved forward, I just shoot this reminder in order to help improving the plugin.
Here are the details.
{
"tns-ios": {
"version": "2.5.0"
},
"tns-android": {
"version": "2.5.0"
}
},
"dependencies": {
"nativescript-local-notifications": "^1.2.1",
"nativescript-push-notifications": "^0.1.2",
"tns-core-modules": "^2.5.0"
}
/**
* Local Notifications setup
*/
var notifications = require( 'nativescript-local-notifications' );
var tap_callbacks = {}; // collection of notification tap callbacks; the key is the notification ID
notifications.addOnMessageReceivedCallback( function ( notification ) {
// This callback is executed on receive of both local and push notifications
if ( tap_callbacks[ notification.id ] ) { // this check solves the problem - push notifications do not register tap callbacks here
tap_callbacks[ notification.id ]( notification );
}
});
module.exports = {
allow : function ( callback ) {
notifications.requestPermission().then( callback );
},
schedule : function ( payload, tap_callback, schedule_callback ) {
payload.id = payload.id || generateID();
typeof tap_callback != 'function' && ( tap_callback = function () {} );
typeof schedule_callback != 'function' && ( schedule_callback = function () {} );
notifications.schedule( [ payload ] ).then(
function () {
tap_callbacks[ payload.id ] = tap_callback;
schedule_callback( true );
},
function ( err ) {
schedule_callback( false, err.message );
}
);
}
};
var local_notifications = require( './local-notifications' );
// ask user for permission
local_notifications.allow();
// application does not show any local notifications yet
var notifications = require( 'nativescript-push-notifications' );
var device = require( './device' ); // `device` is a custom module that has access to app settings
var settings = {
senderID : '123456789012', // Android
// iOS settings
badge : true, // Enable setting badge through Push Notification
sound : true, // Enable playing a sound
alert : true, // Enable creating a alert
notificationCallbackIOS : onNotification,
notificationCallbackAndroid : onNotification,
};
notifications.register(
settings,
// Success
function ( token ) {
if ( notifications.onMessageReceived ) {
// Android
notifications.onMessageReceived( settings.notificationCallbackAndroid );
}
// Keep the token in app settings and post it to the service
device.setToken( token );
},
// Error
function ( error ) {
alert( error );
}
);
function onNotification( json ) {
// currently this callback is empty
}
Push notifications are sent via node-apn module (NodeJS), version 2.1.3 .
var apn = require( 'apn' );
var providers = {
apn : new apn.Provider({
token : {
key : './certificates/apn.p8',
keyId : 'XXXXXXXXXX',
teamId : 'XXXXXXXXXX'
},
production : false
}),
};
module.exports = {
send : sendAPN
};
function sendAPN( token, title, message, callback, options ) {
callback = typeof callback == 'function' ? callback : function () {};
providers.apn.send( createNote( title, message, options ), token ).then( function ( response ) {
callback( !response.failed.length, token );
});
}
function createNote( title, message, options ) {
options = options || {};
var note = new apn.Notification();
note.expiry = options.expires || Math.floor( Date.now() / 1000 ) + 3600; // Expires 1 hour from now.
note.badge = 1;
note.sound = 'ping.aiff';
note.alert = message;
note.payload = { messageFrom : title };
note.topic = 'xxx.xxx.xxxx';
return note;
}
require( './notifications' ).send( 'device-token', 'Title', 'Message' );
Let me know if you need anything else.
Hi,
iOS app crashes:
tns version 2.5.0, plugin version 0.1.2
Crash log: