我使用这个插件的离子:
https://github.com/salesforce-marketingcloud/MC-Cordova-Plugin
我让它在iOS上运行,但在android上似乎插件甚至不存在。我认为是因为我不是很擅长棱角分明,所以我会如何称呼它。
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 {}
我的项目有一个模块:
-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
推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;
}
推notifications.models.ts:
export interface Attributes {
[key: string]: string
}
推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';
推notifications.module.ts:
import { NgModule } from "@angular/core";
import { PushNotificationsService } from "./";
@NgModule({
providers: [
PushNotificationsService
]
})
export class PushNotificationsModule {}
最后我的离子页面,我称之为插件:
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周,如此,我一无所知,请帮忙,任何想法?
谢谢你们,祝你有愉快的一天!
好吧,谢谢你的downvotes哈哈,我终于解决了,如果有人正在处理这个,这里是解决方案:
我有我的android构建:
(见命令:离子信息)
显然,该版本和MC-Cordova插件破坏了一些东西。
解决方案:降级cordova版本:
完成!