phonegap / phonegap-plugin-push

Register and receive push notifications
MIT License
1.94k stars 1.91k forks source link

Property 'createChannel' does not exist on type 'PushNotificationStatic'. #2433

Open barmarko27 opened 6 years ago

barmarko27 commented 6 years ago

Expected Behaviour

Implementation of channel in ionic2 app

Actual Behaviour

Method createChannel not found in PushNotificationStatic

Reproduce Scenario (including but not limited to)

Added the createChannel before call init method into my app

Steps to Reproduce

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

Android 8.0.0

(Android) What device vendor (e.g. Samsung, HTC, Sony...)

Nexus 6P

Cordova CLI version and cordova platform version

`cordova --version                                    8.0.0`
`cordova platform version android                     6.3.0`

Plugin version

`cordova plugin version | grep phonegap-plugin-push   2.1.2`

Sample Push Data Payload

{ "registration_ids": [""], "data": { "message": "Test message", "title": "Title", "soundname": "sounddefault", // for Android versions < 8.0 (API Level < 26) "android_channel_id": "reminders", } }

Sample Code that illustrates the problem

var channelId = "reminders";

PushNotification.createChannel(
  function() { console.log("Successfully created notification channel: " + channelId); },
  function() { console.log("Failed while attempting to create notification channel: " + channelId); },
  {
      id: channelId,
      description: "Reminders",
      importance: 4, // high importance
      sound: "sounddefault"  // plays alarm_1.wav in the res/raw folder
  }
);

let push = PushNotification.init({
  android: {
    senderID: 'XXXXXXXXXXXXX',
    forceShow: true
  },
  ios: {
    alert: "true",
    badge: true,
    sound: 'true'
  },
  windows: {}
});

Logs taken while reproducing problem

Property 'createChannel' does not exist on type 'PushNotificationStatic'. 

  L34:      PushNotification.createChannel(
  L35:        function() { console.log("Successfully created notification channel: " + channelId); },

 Error: Failed to transpile program
at new BuildError (/Users/mobile-dev/myapp/node_modules/@ionic/app-scripts/dist/util/errors.js:16:28)
at /Users/mobile-dev/myapp/node_modules/@ionic/app-scripts/dist/transpile.js:137:20
at new Promise (<anonymous>)
at transpileWorker (/Users/mobile-dev/myapp/node_modules/@ionic/app-scripts/dist/transpile.js:103:12)
at Object.transpile (/Users/mobile-dev/myapp/node_modules/@ionic/app-scripts/dist/transpile.js:61:12)
at buildProject (/Users/mobile-dev/myapp/node_modules/@ionic/app-scripts/dist/build.js:97:78)
at /Users/mobile-dev/myapp/node_modules/@ionic/app-scripts/dist/build.js:47:16
at <anonymous>`
fredgalvao commented 6 years ago

That is because the file at phonegap-plugin-push/types/index.d.ts which has the type definition for this plugin's API has not been updated to include the createChannel method.

That was supposed to be a task assigned to me, but I haven't found the time to go back to it and do it recently, for there are a few other points I need to review to fully sync the typings with the API proper. PRs are welcome ;).

A sad workaround is to cast the PushNotification object to <any> on that call.

barmarko27 commented 6 years ago

Hi, thanks for your feedback!

I've tried in this way, but the app don't start. In the Google Chrome console I got this error: main.js:1507 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'createChannel' of undefined TypeError: Cannot read property 'createChannel' of undefined

` var channelId = "reminders";

var PushNotification: any;

PushNotification.createChannel(
  function() { console.log("Successfully created notification channel: " + channelId); },
  function() { console.log("Failed while attempting to create notification channel: " + channelId); },
  {
      id: channelId,
      description: "Reminders",
      importance: 4, // high importance
      sound: "sounddefault"  // plays alarm_1.wav in the res/raw folder
  }
);    

let push = PushNotification.init({
  android: {
    senderID: 'XXXXXXXXXXX',
    forceShow: true
  },
  ios: {
    alert: "true",
    badge: true,
    sound: 'true'
  },
  windows: {}
});`
fredgalvao commented 6 years ago

When you do var PushNotification: any;, you're overriding/hiding the original value of the global PushNotification object with undefined (your variable is unassigned).

Try var PushNotification: any = <any> PushNotification;.

uj commented 6 years ago

I am having a similar issue, except my problem occurs at runtime instead of during building. I am using (this.Push as any).createChannel(..), and that allows the project to build without errors, but when I run the app, I get "TypeError: this.Push.createChannel is not a function"

Just to make sure I'm not messing up anything with the way I cast Push to any, I removed the createChannel function from my code and did (this.Push as any).init(..) and that runs just fine, so I know that the any cast isn't messing it up, because the .init function works properly that way.

As an alternate method, I used "var PushNotification: any = PushNotification;" instead of the inline cast, which again allowed it to build without error, but I still receive the error "TypeError: this.Push.createChannel is not a function" when the app runs.

Perhaps this simply will not work at all until the types/index.d.ts file is updated?

fredgalvao commented 6 years ago

I'm not sure your issue is exactly the same, @uj. Casting the PushNotification object to <any> should allow any call/access on it to fall through to the original value that goes on the javascript runtime, which should always work because there's no type information whatsoever leftover from typescript during runtime (typescript is a compile-time-only language).

Anyway, that's just my guess. We need to update the type definitions soon, regardless of anything.