/**
 * Created by youyou on 16/11/23.
 */ import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs/Rx' ; declare var plugins ; declare var document ; @ Injectable ( ) export class JPushService { private jPushPlugin : any ; warnText : string = '没有找到 jPushPlugin 对象 \n也许你是在浏览器环境下运行或者没有正确安装插件； \n如果没有在Platform 的 ready 方法中调用，也会出现这样的情况。\n了解：http://ionicframework.com/docs/v2/api/platform/Platform/' ; constructor ( ) { this . initJPushPlugin ( ) ; } wrapEventObservable ( event ) { return new Observable ( ( observer ) => { document . addEventListener ( event , observer . next . bind ( observer ) , false ) ; return ( ) => document . removeEventListener ( event , observer . next . bind ( observer ) , false ) ; } ) ; } initJPushPlugin ( ) { if ( ( < any > window ) . plugins && ( < any > window ) . plugins . jPushPlugin ) { this . jPushPlugin = ( < any > window ) . plugins . jPushPlugin ; } else if ( ( < any > window ) . JPush ) { this . jPushPlugin = ( < any > window ) . JPush ; } else { this . jPushPlugin = null ; } } setDebugMode ( debug ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . setDebugMode ( debug ) ; resolve ( 'ok' ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } startJPushSDK ( ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . startJPushSDK ( ) ; resolve ( 'ok' ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } init ( ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . init ( ) ; resolve ( 'ok' ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } stopPush ( ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . stopPush ( ) ; resolve ( 'ok' ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } resumePush ( ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . resumePush ( ) ; resolve ( 'ok' ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } getRegistrationID ( ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . getRegistrationID ( ( id ) => { if ( id ) { resolve ( id ) ; } else { reject ( '获取ID失败' ) ; } } ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } getUserNotificationSettings ( ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . getUserNotificationSettings ( ( result ) => { if ( result === 0 ) { reject ( result ) ; } else { resolve ( result ) ; } } ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } setLatestNotificationNum ( num = 5 ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . setLatestNotificationNum ( num ) ; resolve ( 'ok' ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } isPushStopped ( ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . isPushStopped ( ( result ) => { resolve ( result ) ; } ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } param tags param alias returns { Promise < T > } setTagsWithAlias ( tags , alias ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . setTagsWithAlias ( tags , alias ) ; resolve ( 'ok' ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } param data returns { Promise < T > } setTags ( data ) { this . initJPushPlugin ( ) ; if ( Array . isArray ( data ) ) { return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . setTags ( data ) ; resolve ( 'ok' ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } else if ( typeof data === 'object' ) { return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . setTags ( data , ( res ) => resolve ( res ) , ( error ) => reject ( error ) ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } } addTags ( data ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . addTags ( data , ( res ) => resolve ( res ) , ( error ) => reject ( error ) ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } deleteTags ( data ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . deleteTags ( data , ( res ) => resolve ( res ) , ( error ) => reject ( error ) ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } cleanTags ( data ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . cleanTags ( data , ( res ) => resolve ( res ) , ( error ) => reject ( error ) ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } getAllTags ( data ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . getAllTags ( data , ( res ) => resolve ( res ) , ( error ) => reject ( error ) ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } checkTagBindState ( data ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . checkTagBindState ( data , ( res ) => resolve ( res ) , ( error ) => reject ( error ) ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } param alias returns { Promise < T > } setAlias ( alias ) { this . initJPushPlugin ( ) ; if ( typeof alias === 'string' ) { return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . setAlias ( alias ) ; resolve ( 'ok' ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } else if ( typeof alias === 'object' ) { return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . setAlias ( alias , ( res ) => resolve ( res ) , ( error ) => reject ( error ) ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } } deleteAlias ( data ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . deleteAlias ( data , ( res ) => resolve ( res ) , ( error ) => reject ( error ) ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } getAlias ( data ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . getAlias ( data , ( res ) => resolve ( res ) , ( error ) => reject ( error ) ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } param value returns { Promise < T > } setBadge ( value ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . setBadge ( value ) ; resolve ( 'ok' ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } reSetBadge ( ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . reSetBadge ( ) ; resolve ( 'ok' ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } clearAllLocalNotifications ( ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . clearAllLocalNotifications ( ) ; resolve ( 'ok' ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } param value returns { Promise < T > } setApplicationIconBadgeNumber ( value ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . setApplicationIconBadgeNumber ( value ) ; resolve ( 'ok' ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } getApplicationIconBadgeNumber ( ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . getApplicationIconBadgeNumber ( ( num ) => { resolve ( num ) ; } ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } setPushTime ( days , startHour , endHour ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . setPushTime ( days , startHour , endHour ) ; resolve ( 'ok' ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } setSilenceTime ( startHour , startMinute , endHour , endMinute ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . setSilenceTime ( startHour , startMinute , endHour , endMinute ) ; resolve ( 'ok' ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } param id returns { Promise < T > } clearNotificationById ( id ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . clearNotificationById ( id ) ; resolve ( 'ok' ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } clearAllNotification ( ) { this . initJPushPlugin ( ) ; return new Promise ( ( resolve , reject ) => { if ( this . jPushPlugin ) { this . jPushPlugin . clearAllNotification ( ) ; resolve ( 'ok' ) ; } else { console . warn ( this . warnText ) ; reject ( '没有找到 jPushPlugin' ) ; } } ) ; } returns { Observable < any > } openNotification ( ) { return this . wrapEventObservable ( 'jpush.openNotification' ) ; } returns { Observable < any > } receiveNotification ( ) { return this . wrapEventObservable ( 'jpush.receiveNotification' ) ; } returns { Observable < any > } receiveMessage ( ) { return this . wrapEventObservable ( 'jpush.receiveMessage' ) ; } backgroundNotification ( ) { return this . wrapEventObservable ( 'jpush.backgroundNotification' ) ; } receiveRegistrationId ( ) { return this . wrapEventObservable ( 'jpush.receiveRegistrationId' ) ; } }