首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >MC-Cordova-Plugin不工作[离子角度]

MC-Cordova-Plugin不工作[离子角度]
EN

Stack Overflow用户
提问于 2018-08-03 11:13:45
回答 1查看 579关注 0票数 0

我正在使用ionic的这个插件:

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

我让它在iOS上运行,但在android上,这个插件似乎根本不存在。我想是关于我是如何调用插件的,因为我不太擅长angular。

app.module.ts:

代码语言:javascript
复制
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 {}

我的proyect上有一个模块:

代码语言:javascript
复制
-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 fications.interface.ts:

代码语言:javascript
复制
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 fications.models.ts:

代码语言:javascript
复制
export interface Attributes {
  [key: string]: string
}

推送-通知.service.ts:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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 fications.module.ts:

代码语言:javascript
复制
import { NgModule } from "@angular/core";
import { PushNotificationsService } from "./";

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

最后是我的离子页面,我将其称为插件:

代码语言:javascript
复制
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

}

所以..。在ios上它可以工作,在android上就不行了。

在android上,"system.log()","alert()“甚至没有显示,没有错误,任何东西,我已经尝试了3个星期了,我在这一点上一无所知,请帮帮忙,有什么想法吗?

谢谢你们,祝你们愉快!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-04 08:16:59

好了,谢谢你的反对哈哈,我终于解决了,如果有人正在处理这个问题,这是解决方案:

我的android构建是这样的:

  • cordova 8.0.0
  • cordova-android 7.0.0

(请使用以下命令查看您的: ionic info)

所以很明显这个版本和MC-Cordova-plugin出了点问题。

解决方案:降级cordova版本:

cordova cordova rm

  1. npm卸载-g cordova
  2. npm安装cordova@7.0.0
  3. ionic cordova平台添加android@6.3.0
  4. ionic cordova android
  5. ionic cordova -g

完成了!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51664802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档