【发布时间】:2018-06-03 05:19:29
【问题描述】:
我必须在我的应用程序中使用推送通知,所以我按照this tutorial 添加了离子原生推送。
当我在 Android 控制台上运行应用程序时会打印以下警告消息:
Native:尝试调用 PushNotification.init,但 PushNotification 插件未安装。
我已经安装了模块:
$ ionic cordova plugin add phonegap-plugin-push
$ npm install --save @ionic-native/push
怎么可能?
我的代码在这里:
我将提供程序添加到我的 app.module.ts:
import { Push } from '@ionic-native/push';
...
providers: [
StatusBar,
SplashScreen,
Services,
Globals,
Push,
Globalization,
Facebook,
GoogleMaps,
Geolocation,
{provide: ErrorHandler, useClass: IonicErrorHandler},
{ provide: LOCALE_ID, useValue: 'es' },
{ provide: HAMMER_GESTURE_CONFIG, useClass: MyHammerConfig }
]
在我的 app.component.ts 我有这个:
import { Component } from '@angular/core';
import { Platform, ModalController, MenuController, App, AlertController } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { Push, PushObject, PushOptions } from '@ionic-native/push';
import { TabsPage } from '../pages/tabs/tabs';
import { LogregPage } from '../pages/logreg/logreg';
import { FavoritosPage } from '../pages/favoritos/favoritos';
import { MaptestPage } from '../pages/maptest/maptest';
import { Globals } from '../providers/globals';
import { Services } from '../providers/services';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage:any;
// rootPage:any = TabsPage;
// rootPage:any = LogregPage;
constructor(public appCtrl: App, public platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, public modalCtrl: ModalController,
private push: Push, public menuCtrl: MenuController, public globals: Globals, private services: Services, public alertCtrl: AlertController) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
/// Here we try to start the push services
this.initPushNotification();
/// This checks if user session is open
this.globals.checkSession(
(checked) : void => {
if ( checked ) {
this.rootPage = TabsPage;
} else {
this.rootPage = LogregPage;
}
}
);
});
}
/**
* This method is the same as the example :)
*
*/
initPushNotification() {
if (!this.platform.is('cordova')) {
console.warn('Push notifications not initialized. Cordova is not available - Run in physical device');
return;
}
const options: PushOptions = {
android: {
senderID: 'YOUR_SENDER_ID'
},
ios: {
alert: 'true',
badge: false,
sound: 'true'
},
windows: {}
};
const pushObject: PushObject = this.push.init(options);
pushObject.on('registration').subscribe((data: any) => {
console.log('device token -> ' + data.registrationId);
//TODO - send device token to server
});
pushObject.on('notification').subscribe((data: any) => {
console.log('message -> ' + data.message);
//if user using app and push notification comes
if (data.additionalData.foreground) {
// if application open, show popup
let confirmAlert = this.alertCtrl.create({
title: 'New Notification',
message: data.message,
buttons: [{
text: 'Ignore',
role: 'cancel'
}, {
text: 'View',
handler: () => {
//TODO: Your logic here
console.log('Push notification recived');
}
}]
});
confirmAlert.present();
} else {
//if user NOT using app and push notification comes
//TODO: Your logic on click of push notification directly
console.log('Push notification clicked');
}
});
pushObject.on('error').subscribe(error => console.error('Error with Push plugin' + error));
}
}
编辑:
删除然后添加android平台后,运行应用程序时出现此错误:
BUILD SUCCESSFUL
Total time: 3.207 secs
Subproject Path: CordovaLib
Starting a Gradle Daemon (subsequent builds will be faster)
+-----------------------------------------------------------------
| cordova-android-support-gradle-release: 26.+
+-----------------------------------------------------------------
The Task.leftShift(Closure) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use Task.doLast(Action) instead.
at build_9ug1ggkhhrzygsw0k34tph6ua.run(/Users/[edited path]/platforms/android/build.gradle:143)
FAILURE: Build failed with an exception.
* Where:
Script '/Users/[edited path]/platforms/android/phonegap-plugin-push/tusclases-push.gradle' line: 35
* What went wrong:
A problem occurred evaluating root project 'android'.
> Failed to apply plugin [class 'com.google.gms.googleservices.GoogleServicesPlugin']
> For input string: "+"
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 5.408 secs
Error: /Users/[edited path]/platforms/android/gradlew: Command failed with exit code 1 Error output:
FAILURE: Build failed with an exception.
* Where:
Script '/Users/[edited path]/platforms/android/phonegap-plugin-push/tusclases-push.gradle' line: 35
* What went wrong:
A problem occurred evaluating root project 'android'.
> Failed to apply plugin [class 'com.google.gms.googleservices.GoogleServicesPlugin']
> For input string: "+"
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
[ERROR] An error occurred while running cordova run android (exit code 1).
【问题讨论】:
标签: ionic-framework push-notification ionic3