arleyandrada / PushClient

FREE and Open Source - Titanium PushClient Module
Other
44 stars 17 forks source link

[Android][5.4.0.GA] - Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on a null object reference #147

Closed DouglasHennrich closed 7 years ago

DouglasHennrich commented 7 years ago
[ERROR] TiExceptionHandler: (main) [1381,1381] ----- Titanium Javascript Runtime Error -----
[ERROR] TiExceptionHandler: (main) [0,1381] - In core/pushnotification.js:1,69
[ERROR] TiExceptionHandler: (main) [0,1381] - Message: Uncaught Error: Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on a null object reference
[ERROR] TiExceptionHandler: (main) [0,1381] - Source: .EVENT_CALLBACK,eventCallback),module.exports=function(){PushClient.registerPu
[ERROR] V8Exception: Exception occurred at core/pushnotification.js:1: Uncaught Error: Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on a null object reference

core/pushnotification.js:

/* ===============================
@ Dependencies
=============================== */
var PushClient = require('br.com.arlsoft.pushclient')
  , Provider = require('core/oneSignal')
  ;

/* ===============================
@ Private
=============================== */
var registerOptions = {
  GCMSenderId: Alloy.CFG.GCMSenderId
  , APNTypes: [
    PushClient.NOTIFICATION_TYPE_BADGE
    , PushClient.NOTIFICATION_TYPE_ALERT
    , PushClient.NOTIFICATION_TYPE_SOUND
  ]
};

//Workaround to Titanium Mobile issue #17030
//https://jira.appcelerator.org/browse/TIMOB-17030
if (!OS_ANDROID) {
    var issue17030 = Ti.Network.registerForPushNotifications;
    var issue17030iOS8 = Ti.App.iOS.registerUserNotificationSettings;
}

// If we want to send content
// if (OS_IOS) {
//  var acceptAction = PushClient.createAction({
//      identifier: 'ACCEPT_IDENTIFIER',
//      title: 'Accept',
//      activationMode: PushClient.NOTIFICATION_ACTIVATION_MODE_FOREGROUND,
//      destructive: false,
//      authenticationRequired: true
//  });
//  var rejectAction = PushClient.createAction({
//      identifier: 'REJECT_IDENTIFIER',
//      title: 'Reject',
//      activationMode: PushClient.NOTIFICATION_ACTIVATION_MODE_BACKGROUND,
//      destructive: true,
//      authenticationRequired: false
//  });
//  var downloadContent = PushClient.createCategory({
//    identifier: 'APPROVE_CONTENT',
//    actionsForMinimalContext: [acceptAction, rejectAction],
//    actionsForDefaultContext: [acceptAction, rejectAction]
//  });
//  registerOptions.Categories = [downloadContent];
// }

/*
@ eventSuccess
*/
function eventSuccess(event){
  console.log('');
  console.warn('- - - - - - - -');
  console.warn('[PUSH - Success] token: ', event.registrationId);
  console.warn('- - - - - - - -');
  console.log('');

  // Add the token to our User's Model
  Alloy.User.UpdateModel({
    deviceToken: event.registrationId
  });

  Alloy.User.RegisterPush(true, event.registrationId);
}

/*
@ eventError
*/
function eventError(event){
  console.log('');
  console.error('- - - - - - - -');
  console.error('[PUSH - Error] error: ', JSON.stringify(event));
  console.error('- - - - - - - -');
  console.log('');

  switch (event.code) {
    case PushClient.ERROR_SENDER_ID:
      // Only for Google Cloud Messaging (Android)
      console.warn('Error:\n\nUndefined GCMSenderId');
      break;

    case PushClient.ERROR_PLAY_SERVICES:
      // Only for Google Cloud Messaging (Android)
      console.warn('Error:\n\nGoogle Play Services not available\n\n' + event.error);
      break;

    case PushClient.ERROR_NOT_SUPPORTED:
      // Possible error messages for iOS:
      // - "Unable to run with iOS Simulator"
      // - "Unable to run with iOS in DEBUG mode"
      // - "Unable to run with iOS DEV profile due to Titanium Mobile issue
      // #17030"
      // Possible error messages for Android:
      // - "This device is not supported"
      console.warn('Error:\n\nNot supported error\n\n' + event.error);
      break;

    case PushClient.ERROR_REGISTER:
      console.warn('Error:\n\nUnable to register this device\n\n' + event.error);
      break;

    case PushClient.ERROR_UNREGISTER:
      console.warn('Error:\n\nUnable to unregister this device\n\n' + event.error);
      break;

    default:
      console.warn('Error:\n\nUnknown error\n\n' + JSON.stringify(event));
      // Should never happen...
  }
}

/*
@ eventCallback
*/
function eventCallback(event){
  console.log('');
  console.warn('- - - - - - - -');
  console.warn('[PUSH - Received] event: ', JSON.stringify(event));
  console.warn('- - - - - - - -');
  console.log('');

  if (!event) {
        console.warn('Callback:\n\nInvalid callback');
        // Should never happen...
    } else {
        switch (event.mode) {
            case PushClient.MODE_FOREGROUND: // case for handling foreground in android
            case 1: // ios modeclick case tested for onesignal as it returns numbers
            // Push data received with app in foreground
            if(OS_ANDROID){
                PushClient.showLocalNotification(event.data);
            }
            console.warn('Callback in Foreground:\n\n' + (JSON.stringify(JSON.parse(event.data.custom).a)));
            break;

      case PushClient.MODE_CLICK: // android mode click case
            case 2: // ios modeclick case tested for onesignal
                console.warn('Callback from Click:\n\n' + (OS_ANDROID) ? (JSON.stringify(JSON.parse(event.data.custom).a)) : JSON.stringify(event.data.custom.a));
                if(OS_ANDROID){
                    // android has default event.data.custom as an string format
                    if (Provider.confirmPushOpened && event.data && event.data.custom && JSON.parse(event.data.custom).i) {
                        Provider.confirmPushOpened(JSON.parse(event.data.custom).i, function(error, response) {
                            if (error) {
                                Ti.API.info('Provider API Track Error:\n\n' + JSON.stringify(response));
                            } else {
                                Ti.API.info('Provider API Track Success:\n\n' + JSON.stringify(response));
                            }
                        });
                    }

                } else{
                    // the ios has default event.data.custom as an object format
                    if (Provider.confirmPushOpened && event.data && event.data.custom && event.data.custom.i) {
                        Provider.confirmPushOpened(event.data.custom.i, function(error, response) {
                            if (error) {
                                Ti.API.info('Provider API Track Error:\n\n' + JSON.stringify(response));
                            } else {
                                Ti.API.info('Provider API Track Success:\n\n' + JSON.stringify(response));
                            }
                        });
                    }
                }
                break;

            case PushClient.MODE_BACKGROUND:
            case 3: // ios modeclick case tested for onesignal
                // Requires set remote-notification UIBackgroundModes in tiapp.xml
                PushClient.endBackgroundHandler(event.data.handlerId);
                // Put the application back to sleep before any UI interations
                console.warn('Callback from Silent:\n\n' + JSON.stringify(event.data));
                // Push data received with app in background
                break;

            case PushClient.MODE_ACTION:
            case 4: // ios modeclick case tested for onesignal
                console.warn('Callback from Action:\n\n'+event.category+'\n'+event.identifier+'\n\n' + JSON.stringify(event.data));
                // Push data received when user choose an action from notification message
                break;

            default:
                console.warn('Callback:\n\n' + JSON.stringify(event.data));
                // Should never happen...
        }
    }
}

/* ===============================
@ Listeners
=============================== */
PushClient.addEventListener( PushClient.EVENT_SUCCESS, eventSuccess );
PushClient.addEventListener( PushClient.EVENT_ERROR, eventError );
PushClient.addEventListener( PushClient.EVENT_CALLBACK, eventCallback );

/* ===============================
@ Public
=============================== */
module.exports = function(){
  PushClient.registerPush(registerOptions);
};

/* ===============================
@ Notification properties
=============================== */
// "console.warn" (String) to text message - or "text" to Android
// "sound" (String) to sound file to play - "default" to Android default sound
// "badge" (Int) to iOS badge count indicator
// "content-available" (Int) to iOS background notification
// -->> Requires set remote-notification UIBackgroundModes in tiapp.xml
// "notificationId" (Int) to Android notification identification
// "title" (String) to override Android notification title - default app name
// "ticker" (String) to override Android ticker message - default console.warn message
// "vibrate" (Boolean) to use default Android device vibration
// "lights" (Boolean) to use default Android device lights
// "smallIcon" (String) set small icon to Android notification from assets
// "largeIcon" (String) set large icon to Android notification from local file or url
// "category" (String) to iOS 8 interactive notification
// "ledARGB" (Color) to change default Android device light color
// "ledOnMS" (Int) to change default Android device light blink on time
// "ledOffMS" (Int) to change default Android device light blink off time

I'm running version 1.15

Don't know why I'm doing wrong =/

DouglasHennrich commented 7 years ago

nevermind... for some reason my registrationOptions isn't getting my GCM Sender Id from config.json:

{"APNTypes":[1,2,3]}