danielsogl / awesome-cordova-plugins

Native features for mobile apps built with Cordova/PhoneGap and open web technologies. Complete with TypeScript support.
https://danielsogl.gitbook.io/awesome-cordova-plugins/
Other
2.41k stars 2.42k forks source link

TS typing trouble with the ILocalNotification interface #2443

Closed alecrem closed 6 years ago

alecrem commented 6 years ago

I'm submitting a ... (check one with "x") [x] bug report [ ] feature request [ ] support request => Please do not submit support requests here, use one of these channels: https://forum.ionicframework.com/ or https://ionicworldwide.herokuapp.com/

Current behavior: My iOS app will work on livereload, so I think my copy of the Cordova plugin works with trigger, but the transpiler won't allow the property so I can't actually deploy the app. It seems to me that the ILocalNotification Interface in @ionic-native/local-notifications/index.d.ts doesn't contemplate the trigger property because it's a newer object structure.

Argument of type '{ id: number; title: string; text: string; trigger: { in: number; unit: string; }; }' is not assignable to parameter of type 'ILocalNotification | ILocalNotification[]'. Object literal may only specify known properties, and 'trigger' does not exist in type 'ILocalNotification | ILocalNotification[]'.

Argument of type '{ id: number; title: string; text: string; trigger: { at: Date; }; }' is not assignable to parameter of type 'ILocalNotification | ILocalNotification[]'. Object literal may only specify known properties, and 'trigger' does not exist in type 'ILocalNotification | ILocalNotification[]'.

Expected behavior: I should be able to schedule a local notification using the trigger property, or the examples on the Ionic Native Docs should be updated with the current right way to use the plugin.

Steps to reproduce:

  1. Install the plugin following the instructions on the Ionic Native Docs
  2. Add it to the app's module
  3. Import and add it to the constructor for the page's module
  4. Try to schedule a delayed notification with a trigger on the page's module

Related code: Example from the Ionic Native Docs, using trigger:

this.localNotifications.schedule({
   text: 'Delayed ILocalNotification',
   trigger: {at: new Date(new Date().getTime() + 3600)},
   led: 'FF0000',
   sound: null
});

Interface from @ionic-native/local-notifications/index.d.ts, doesn't have the trigger property in it:

export interface ILocalNotification {
    /**
     * A unique identifier required to clear, cancel, update or retrieve the local notification in the future
     * Default: 0
     */
    id?: number;
    /**
     * First row of the notification
     * Default: Empty string (iOS) or the app name (Android)
     */
    title?: string;
    /**
     * Second row of the notification
     * Default: Empty string
     */
    text?: string;
    /**
     * The interval at which to reschedule the local notification. That can be a value of second, minute, hour, day, week, month or year
     * Default: 0 (which means that the system triggers the local notification once)
     */
    every?: string;
    /**
     * The date and time when the system should deliver the local notification. If the specified value is nil or is a date in the past, the local notification is delivered immediately.
     * Default: now ~ new Date()
     */
    at?: any;
    firstAt?: any;
    /**
     * The number currently set as the badge of the app icon in Springboard (iOS) or at the right-hand side of the local notification (Android)
     * Default: 0 (which means don't show a number)
     */
    badge?: number;
    /**
     * Uri of the file containing the sound to play when an alert is displayed
     * Default: res://platform_default
     */
    sound?: string;
    /**
     * Arbitrary data, objects will be encoded to JSON string
     * Default: null
     */
    data?: any;
    /**
     * ANDROID ONLY
     * Uri of the icon that is shown in the ticker and notification
     * Default: res://icon
     */
    icon?: string;
    /**
     * ANDROID ONLY
     * Uri of the resource (only res://) to use in the notification layouts. Different classes of devices may return different sizes
     * Default: res://ic_popup_reminder
     */
    smallIcon?: string;
    /**
    * ANDROID ONLY
    * RGB value for the background color of the smallIcon.
    * Default: Androids COLOR_DEFAULT, which will vary based on Android version.
    */
    color?: string;
    /**
     * ANDROID ONLY
     * Ongoing notifications differ from regular notifications in the following ways:
     * - They are sorted above the regular notifications in the notification panel
     * - They do not have an 'X' close button, and are not affected by the "Clear all" button
     * Default: false
     */
    ongoing?: boolean;
    /**
     * ANDROID ONLY
     * ARGB value that you would like the LED on the device to blink
     * Default: FFFFFF
     */
    led?: string;
    /**
    * Notification priority.
    */
    priority?: number;
    /**
     * Is a silent notification
     */
    silent?: boolean;
}

Other information: @mowaiskalam seems to be having the same issue.

Ionic info: (run ionic info from a terminal/cmd prompt and paste output below):

cli packages: (/usr/local/lib/node_modules)

    @ionic/cli-utils  : 1.19.1
    ionic (Ionic CLI) : 3.19.1

global packages:

    cordova (Cordova CLI) : 7.1.0

local packages:

    @ionic/app-scripts : 3.1.2
    Cordova Platforms  : android 6.3.0 ios 4.5.4
    Ionic Framework    : ionic-angular 3.9.2

System:

    ios-deploy : 1.9.2
    ios-sim    : 6.1.2
    Node       : v8.9.3
    npm        : 5.7.1
    OS         : macOS Sierra
    Xcode      : Xcode 9.2 Build version 9C40b

Environment Variables:

    ANDROID_HOME : not set

Misc:

    backend : pro
alecrem commented 6 years ago

Adding the following to the ILocalNotification interface allowed me to properly build the app, but I don't know if it should be more specific:

    trigger?: any;
alecrem commented 6 years ago

I still don't know how I got the wrong version, and there seem to be other people finding the same issue. Hope this can help someone. This fixed it:

npm update @ionic-native/local-notifications
mowaiskalam commented 6 years ago

Thanks, @alecrem. It worked for me as well. But now i'm getting typescript compilation error. "In ambient enum declarations member initializer must be constant expression."

mowaiskalam commented 6 years ago

Resolved that as well by updating typescript.

biranchi2018 commented 6 years ago

Neither of the above solutions worked.

Neither

npm update @ionic-native/local-notifications

nor

npm install typescript@latest -g

worked for me.

Below is my ionic info

ionic info:

cli packages: (/usr/local/lib/node_modules)

@ionic/cli-utils  : 1.19.2
ionic (Ionic CLI) : 3.20.0

global packages:

cordova (Cordova CLI) : not installed

local packages:

@ionic/app-scripts : 3.1.8
Cordova Platforms  : none
Ionic Framework    : ionic-angular 3.9.2

System:

ios-deploy : 1.9.2 
Node       : v6.14.0
npm        : 3.10.10 
OS         : macOS High Sierra
Xcode      : Xcode 9.3 Build version 9E145 

Environment Variables:

ANDROID_HOME : ~/Library/Android/sdk

Misc:

backend : pro
marioshtika commented 6 years ago

I am still having this issue. Is this fixed? I still can not use trigger: { every: { month: 10, day: 27, hour: 9, minute: 0 } } beacuase type of every only expects ELocalNotificationTriggerUnit which an enum for string not an object.

maxfloden commented 4 years ago

If anyone is still struggling with this, this works for me (after some hair-pulling): Instead of 'second' specify ELocalNotificationTriggerUnit.SECOND Same should go for other triggers such as "every".

    this.localNotifications.schedule({
      id: 123,
      foreground: true,
      text: '5 sec test',
      trigger: {in:5,unit:ELocalNotificationTriggerUnit.SECOND} 
    });