0

Im using this plugin for ionic:

https://github.com/salesforce-marketingcloud/MC-Cordova-Plugin

I made it work on iOS, but on android seems like the plugin doesn't even exist. I think is something about how Im calling the pluging since I am not very good at angular.

app.module.ts:

import { PushNotificationsService } from './../modules/somefolder/push-notifications/';

@NgModule({
  declarations: [
    MyApp,
    HomePage,
    InicioPage

  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp),
    IonicStorageModule.forRoot()
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage,
    InicioPage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    PushNotificationsService,
  ]
})
export class AppModule {}

And i have a module on my proyect:

-modules
 |_somefolder
  |_push-notifications
   |_interfaces
   | |_push-notifications.interface.ts
   |
   |_models
   | |_push-notifications.models.ts
   |
   |_providers
   | |_push-notifications.service.ts
   |
   |_index.ts
   |_push-notifications.module.ts

push-notifications.interface.ts:

import { Attributes } from './../';

export interface PushNotificationsInterface {

  // ANDROID AND iOS FEATURES

  // Logging
  enableVerboseLogging(successCallback: () => void, errorCallback: () => void): void;
  disableVerboseLogging(successCallback: () => void, errorCallback: () => void): void;

  // Push
  isPushEnabled(successCallback: (enabled: boolean) => void, errorCallback: () => void): void;

  // Get system token.
  getSystemToken(successCallback: (systemToken: string) => void, errorCallback: () => void): void;

  // Attributes
  getAttributes(successCallback: (attributes: Attributes) => void, errorCallback: () => void): void;
  setAttribute(successCallback: (success: boolean) => void, errorCallback: () => void, key: string, value: string): void;
  clearAttribute(successCallback: (attributeRemoved: string) => void, errorCallback: () => void, key: string): void;

  // ContactKey           
  setContactKey(successCallback: () => void, errorCallback: () => void, contactKey: string): void;
  getContactKey(successCallback: (contactKey: string) => void, errorCallback: () => void): void;

  // Tags           
  addTag(successCallback: () => void, errorCallback: () => void, tag: string): void;
  removeTag(successCallback: () => void, errorCallback: () => void, tag: string): void;
  getTags(successCallback: (tags: Array<string>) => void, errorCallback: () => void): void;

  // ANDROID ONLY FEAURES

  // Push

  enablePush(successCallback: () => void, errorCallback: () => void): void;
  disablePush(successCallback: () => void, errorCallback: () => void): void;

}

push-notifications.models.ts:

export interface Attributes {
  [key: string]: string
}

push-notifications.service.ts:

import { Injectable } from '@angular/core';
import { PushNotificationsInterface } from './../';
import { Attributes } from './../';

declare const MCCordovaPlugin: PushNotificationsInterface;

@Injectable()
export class PushNotificationsService {

  // ANDROID AND iOS FEATURES

  public isPushEnabled(): Promise<boolean> {
    return new Promise((resolve, reject) => {
      MCCordovaPlugin.isPushEnabled(
        (enabled: boolean) => resolve(enabled),
        () => reject('Can Not Determinate If Is Enabled')
      );
    });
  }

  public getContactKey(): Promise<string> {
    return new Promise((resolve, reject) => {
      MCCordovaPlugin.getContactKey(
        (key: string) => resolve(key),
        () => reject(new Error('Can Not Get Contact Key'))
      );
    });
  }

  public setContactKey(contactKey: string): Promise<boolean> {
    return new Promise((resolve, reject) => {
      MCCordovaPlugin.setContactKey(
        () => resolve(true),
        () => reject('Can Not Set Contact Key'),
        contactKey
      );
    });
  }

  public getSystemToken(): Promise<string> {
    return new Promise((resolve, reject) => {
      MCCordovaPlugin.getSystemToken(
        (systemToken: string) => resolve(systemToken),
        () => reject(new Error('Can Not Get System Token'))
      );
    });
  }

  public getAttributes(): Promise<Attributes> {
    return new Promise((resolve, reject) => {
      MCCordovaPlugin.getAttributes(
        (attributes: Attributes) => resolve(attributes),
        () => reject(new Error('Can Not Get Attributes'))
      );
    });
  }

  public setAttribute(key: string, value: string): Promise<boolean> {
    return new Promise((resolve, reject) => {
      MCCordovaPlugin.setAttribute(
        (success: boolean) => resolve(success),
        () => reject(new Error('Can Not Set Attribute')),
        key, value
      );
    });
  }

  public clearAttribute(attribute: string): Promise<string> {
    return new Promise((resolve, reject) => {
      MCCordovaPlugin.clearAttribute(
        (attributeRemoved: string) => resolve(attributeRemoved),
        () => reject(new Error('Can Not Remove Attribute')),
        attribute
      );
    });
  }

  public getTags(): Promise<Array<string>> {
    return new Promise((resolve, reject) => {
      MCCordovaPlugin.getTags(
        (tags: Array<string>) => resolve(tags),
        () => reject(new Error('Can Not Get Tags'))
      );
    });
  }

  public addTag(tag: string): Promise<any> {
    return new Promise((resolve, reject) => {
      MCCordovaPlugin.addTag(
        () => resolve(),
        () => reject(new Error('Can Not Add Tag')),
        tag
      );
    });
  }

  public removeTag(tag: string): Promise<any> {
    return new Promise((resolve, reject) => {
      MCCordovaPlugin.removeTag(
        () => resolve(),
        () => reject(new Error('Can Not Remove Tag')),
        tag
      );
    });
  }

  // ANDROID ONLY FEAURES

  public enablePush(): Promise<any> {
    return new Promise((resolve, reject) => {
      MCCordovaPlugin.enablePush(
        () => resolve(),
        () => reject(new Error('Can Not Remove Tag'))
      );
    });
  }

  public disablePush(): Promise<any> {
    return new Promise((resolve, reject) => {
      MCCordovaPlugin.disablePush(
        () => resolve(),
        () => reject(new Error('Can Not Remove Tag'))
      );
    });
  }

}

index.ts:

export { PushNotificationsInterface } from './interfaces/push-notifications.interface';
export { Attributes } from './models/push-notifications.models';
export { PushNotificationsService } from './providers/push-notifications.service';
export { PushNotificationsModule } from './push-notifications.module';

push-notifications.module.ts:

import { NgModule } from "@angular/core";
import { PushNotificationsService } from "./";

@NgModule({
  providers: [
    PushNotificationsService
  ]
})
export class PushNotificationsModule {}

And finally my ionic page where i call the plugin:

import { IonicPage, Platform } from 'ionic-angular';
import { Component } from '@angular/core';
import { Attributes, PushNotificationsService  } from './../../modules/cloud-mobile/push-notifications';
import firebase from 'firebase/app';


@IonicPage()
@Component({
  selector: 'page-inicio',
  templateUrl: 'inicio.html',
})
export class InicioPage{

  public pushEnabled: boolean;
  public contactKey: string;
  public systemToken: string;
  public attributes: Array<{$key: string, value: string}>;
  public tags: Array<string>;

  constructor(private pushNotifications: PushNotificationsService, public platform: Platform) {
    this.iniciarPushNotifications();
  }

  iniciarPushNotifications(){
    this.platform.ready().then(() => {
      this.isPushEnabled();
      let user = firebase.auth().currentUser;
      if (user) {
        this.setContactKey(user.email);
        this.getSettings();
      } 
    });
  }


  private getSettings(): void {
    this.getContactKey();
    this.getSystemToken();
    this.getAttributes();
    this.getTags();
  }

  // START MCCordovaPlugin Methods

  private isPushEnabled() {
    this.pushNotifications.isPushEnabled()
      .then((enabled: boolean) => {
        console.log('Push enabled ->' + enabled);
        alert('Push enabled ->' + enabled);
        this.pushEnabled = enabled
      })
      .catch((error: Error) => console.log(error.message));
  }

  private getContactKey(): void {
    this.pushNotifications.getContactKey()
      .then((key: string) => {
        alert(key);
        this.contactKey = key
      })
      .catch((error: Error) => console.log(error.message));
  }

  public setContactKey(ccontactKey): void {  
    this.pushNotifications.setContactKey(ccontactKey)
      .then((success: boolean) => {
          if(success) {
            this.getSettings();
          } else {
            console.log('No se pudo asignar el Contact Key');
          }
      })
      .catch((error: Error) => console.log(error.message));
  }

  private getSystemToken(): void {
    this.pushNotifications.getSystemToken()
      .then((systemToken: string) => {
        alert(systemToken);
        this.systemToken = systemToken
      })
      .catch((error: Error) => console.log(error.message));
  }

  private getAttributes(): void {
    this.attributes = new Array<{$key: string, value: string}>();
    this.pushNotifications.getAttributes()
      .then((attributes: Attributes) => {
        Object.keys(attributes).map((key: string) => this.attributes.push({
          $key: key,
          value: attributes[key]
        }));
      })
      .catch((error: Error) => console.log(error.message));
  }

  public setAttribute(inputKey: any, inputValue: any): void {
    if(inputKey.value && inputValue.value && !this.attributes.find((attribute: {$key: string, value: string}) => 
      attribute.$key == inputKey.value
    )) {
      this.pushNotifications.setAttribute(inputKey.value, inputValue.value)
        .then((success: boolean) => {
          if(success) {
            inputKey.value = '';
            inputValue.value = '';
            this.getAttributes();
          }
        })
        .catch((error: Error) => console.log(error.message));
    }
  }

  public clearAttribute(attrKey: string) {
    this.pushNotifications.clearAttribute(attrKey)
      .then((attributeRemoved: string) => console.log(attributeRemoved))
      .catch((error: Error) => console.log(error.message));
  }

  private getTags(): void {
    this.tags = new Array<string>();
    this.pushNotifications.getTags()
      .then((tags: Array<string>) => {
        this.tags = tags;
      })
      .catch((error: Error) => console.log(error.message));
  }

  public addTag(inputTag: any): void {
    if(!this.tags.find((tag: string) => tag == inputTag.value) && inputTag.value != '') {
      this.pushNotifications.addTag(inputTag.value)
        .then(() => {
          inputTag.value = '';
          this.getTags();
        })
        .catch((error: Error) => console.log(error.message));
    }
  }

  public removeTag(tag: string): void {
    this.pushNotifications.removeTag(tag)
      .then(() => console.log(`Tag ${tag} Removed`))
      .catch((error: Error) => console.log(error.message));
  }

  // END MCCordovaPlugin Methods

}

So... on ios it works, on android dont.

On android the "system.log()", "alert()" doesn't even show, no errors, anything, i have been trying everithing like for 3 weeks now, im clueless at this point, please help, any ideas?

Thank you guys, have a nice day!

1 Answer 1

1

Well, thank you for the downvotes haha, i finally fixed it, if someone is dealing with this, here is the solution:

I had my android build with:

  • cordova 8.0.0
  • cordova-android 7.0.0

(see yours with command: ionic info)

So apparently something is broken with that version and MC-Cordova-plugin.

Solution: Downgrade cordova version:

  1. ionic cordova platform rm android
  2. npm uninstall -g cordova
  3. npm install -g [email protected]
  4. ionic cordova platform add [email protected]
  5. ionic cordova build android
  6. ionic cordova run android

Done!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.