taivo / parse-push-plugin

Push notification plugin for Cordova/Phonegap/ionic on Parse platform
118 stars 102 forks source link

ionic2 - installation not saved #107

Open marcelinne opened 7 years ago

marcelinne commented 7 years ago

Hi I'm using ionic2, but it never creates the installation, is something more what we should change in order to use ionic2 ?, ParsePushPlugin.getInstallationId is working fine, it gets me an id, but when I check in database the "Installation" class continues empty, do you have any idea what it could be? Thanks in advance.

Here is my configuration in parse server:

`

var api = new ParseServer({

databaseURI: databaseUri || 'mongodb://127.0.0.1:27017/dev',

cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',

appId: process.env.APP_ID || 'placeId',

masterKey: process.env.MASTER_KEY || 'secret', 

serverURL: process.env.SERVER_URL || 'http://localhost:1337/api',

push: {

  android: {

    senderId: 'MySenderId',

    apiKey: 'MyApiKey'

  }

}

}); `

My cloud function: `

Parse.Cloud.define('sendPush', function(request, response) {

if (!request.user) return response.error("you are not a user;

var query = new Parse.Query(Parse.Installation);

query.equalTo('userId', request.user);

Parse.Push.send({
  "where": query ,
  "data": {
    "title": "The Shining",
    "alert": "This is a messagey."
  }
}, {
  useMasterKey: true,
  success: function() {
    console.log("push sent");
    response.success('push sent');

  },
  error: function(error) {
    console.log(error);
    response.error(error);
  }
});

}); `

In ionic2 after install parse-push-plugin, I added this to my config.xml: `

<preference name="ParseAppId" value="placeId" />

<preference name="ParseServerUrl" value="http://localhost:1337/parse/" />

<preference name="ParseGcmSenderId" value="MySenderId" />

<preference name="ParseMultiNotifications" value="true" />

<preference name="ParseNotificationIcon" value="icon" />`

In my ts file after the imports: `

declare var ParsePushPlugin: any; `

Then in the constructor class: `

ParsePushPlugin.getInstallationId(function(id) {

            // note that the javascript client has its own installation id,

            // which is different from the device installation id.

            console.log("device installationId: " + id);

            alert("device installationId: " + id);

    }, function(e) {

            console.log('error');

    });

` This last part gets me the installation id, but after check in database none row is created! Please help, I'm missing some configuration? Thanks in advance

cleever commented 7 years ago

Hello,

Have you tried calling manual registration?

Something like this in your app.component.ts:

 if (window['ParsePushPlugin'] !== undefined) {
            let ParsePushPlugin = window['ParsePushPlugin'];
            ParsePushPlugin.register(() => {

            }, (e) => {

            });

or setting up the following line in the config.xml

<preference name="ParseAutoRegistration" value="false" />

marcelinne commented 7 years ago

Thank a lot @cleever for the helping, I've just tried and also it is not working yet :'( .The Installation class continues empty. I'm going to share you my AndroidManifest.xml `

`

cleever commented 7 years ago

Your AndroidManifest.xml seems fine. Did you put the registration inside a platform.ready() function?

Also, what about the following line in your config.xml ?

<preference name="ParseAppId" value="YOUR_APP_ID" />

marcelinne commented 7 years ago

Yes I did put the register function in platdorm.ready() and also a message if it is succesful or there is some error, and the succesful message is thrown, but still no creating the installation.

About the ParseAppId, yes I added in my code, sorry my mistake I did not added above, I'll update.

cleever commented 7 years ago

Ok.

Try to set your clientKey on parse-server, and restart the server. After that, put your clientkey in config.xml:

<preference name="ParseClientKey" value="your-parse-client-key" />

marcelinne commented 7 years ago

It is frustrating, also that way is not working, but shouldn't be requiered as well as clientKey is used with Parse are no longer necessary with Parse Server.

marcelinne commented 7 years ago

After debugging I see that this part of the code is thowing an error (ParsePushApplication.java line79): `

ParseInstallation.getCurrentInstallation().saveInBackground(new SaveCallback() {

    @Override

    public void done(ParseException ex) {

      if (null != ex) {

        Log.e(LOGTAG, ex.toString());

      } else {

        Log.d(LOGTAG, "Installation saved");

      }

    }

  });

`

The error is:

com.parse.ParseRequest$ParseRequestException: bad json response

cleever commented 7 years ago

Strange...

I have ionic1 and ionic2 apps working well with this plugin, with the same configuration:

platform.ready().then(() => {
     if (window['ParsePushPlugin']) {
                ParsePushPlugin.register(function () {

                }, function () {

                });
                ParsePushPlugin.getInstallationId(function (id) {
                    // note that the javascript client has its own installation id,
                    // which is different from the device installation id.
                   alert("device installationId: " + id);
                }, function (e) {
                    alert('error');
                });

At config.xml:

    <preference name="ParseAppId" value="appid" />
    <preference name="ParseClientKey" value="clientid" />
    <preference name="ParseServerUrl" value="serverurl/" /> <-- Do not forget the backslash at the end
    <preference name="ParseGcmSenderId" value="gcmid" />
    <preference name="ParseAutoRegistration" value="true" />
...
<plugin name="parse-push-plugin" spec="https://github.com/taivo/parse-push-plugin" />

That's is all you need to get the plugin working. All my parse instances requires ClientKey.

I think these links may help you: https://github.com/taivo/parse-push-plugin/issues/52 https://github.com/taivo/parse-push-plugin/pull/57 https://github.com/taivo/parse-push-plugin/issues/79

drmark1989 commented 7 years ago

can you please tell me how to import into ts file

marcelinne commented 7 years ago

@drmark1989 if you are using ionic2, the only thing you need is add this line in your ts file:

declare var ParsePushPlugin: any;

The place where you should add is after the imports, like this:

import { AlertController } from 'ionic-angular'; import { LoadingController } from 'ionic-angular'; ...... declare var ParsePushPlugin: any;

That is it! Regards

stevenballs commented 7 years ago

https://stackoverflow.com/questions/46297009/how-to-use-parse-push-notification-with-ionic-2-and-ionic-3