首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获得OneSignal playerID

如何获得OneSignal playerID
EN

Stack Overflow用户
提问于 2018-06-12 08:04:53
回答 1查看 5.7K关注 0票数 1

当用户在启动应用程序时接受通知时,我需要存储playerID。这是关于initializeApp函数在app.component.ts中的。

我可以获得playerID (我可以用console.log显示它),但是当我尝试将它设置为本地存储时,它就不能工作了。在这里,我的app.component.ts:

代码语言:javascript
复制
initializeApp() {
    if (this.platform.ready()) {
      this.platform.ready().then((res) => {
        console.log(res);
        if (res == 'cordova') {
                this.oneSignal.startInit('xxx-xxx-xxx', 'xxx');
                this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.InAppAlert);
                this.oneSignal.getIds().then((ids) => {
                  this.nativeStorage.setItem('playerID', ids.userId);
                });
                this.oneSignal.handleNotificationReceived().subscribe((message) => {
                    // do something when notification is received
               
                });
                this.oneSignal.handleNotificationOpened().subscribe(() => {
                    // do something when a notification is opened
                });

                this.oneSignal.endInit();
                
          // console.log(this.oneSignal.startInit('b8320a40-f565-4593-b9aa-d98b580bf8e1', '323509745034'));
          // this.oneSignal.getIds().then(response=>{
          //   console.log(response);
          //     });
          // this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.InAppAlert);
          // this.oneSignal.handleNotificationReceived().subscribe(() => {
          // });
          // this.oneSignal.handleNotificationOpened().subscribe(() => {
          // });
          // this.oneSignal.endInit();
        }
      });
    }
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-12 10:29:36

在平台准备就绪和初始化完成时将其初始化为OneSignal,您可以获得设备ID:

代码语言:javascript
复制
    import { Platform, } from 'ionic-angular'; // Import Platform
        
    onseSignalAppId: string = 'xxx-xxx-xxx-xxx-xx-x-x-xxx';
    googleProjectId: string = 'xxxxxxxx';
    
    constructor(platform: Platform) {
    }
    
    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();
        
    if (this.platform.is('cordova')) {
    
      if (this.platform.is('android')) {
        this.oneSignal.startInit('onseSignalAppId', 'googleProjectId');
      }
      if (this.platform.is('ios')) {
        this.oneSignal.startInit('onseSignalAppId');
      }
    this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.Notification);

    this.oneSignal.handleNotificationReceived().subscribe(() => {
        // do something when notification is received
      });
    this.oneSignal.handleNotificationOpened().subscribe(result => {
        // do something when a notification is opened
      });
            
      this.oneSignal.endInit();
      
     // Then You Can Get Devices ID

       this.oneSignal.getIds().then(identity => {
           alert(identity.pushToken + " It's Push Token);
           alert(identity.userId + " It's Devices ID");
         });
                       
      });

看吧,文档

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

https://stackoverflow.com/questions/50812150

复制
相关文章

相似问题

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