【问题标题】:ionic 2 push notification not working when app is closed应用程序关闭时,离子 2 推送通知不起作用
【发布时间】:2018-03-15 08:39:40
【问题描述】:

应用程序关闭时我没有收到推送通知。 但它在应用程序处于后台/前台时有效。

这是我在 app.component.ts 中的部分代码

  initPushNotification()
  {
    // to initialize push notifications
    const options: PushOptions = {
       android: {
          icon: 'small-icon',
          forceShow: true
       },
       ios: {
          alert: 'true',
          badge: true,
          sound: 'false'
       },
       windows: {}
    };
    const pushObject: PushObject = this.push.init(options);
    pushObject.on('notification').subscribe((notification: any) => {
      //Notification Display Section
      alert(JSON.stringify(notification));
    });
    pushObject.on('registration').subscribe((registration: any) => {
      alert(registration.registrationId);
    });
    pushObject.on('error').subscribe(error => {
      alert('Error with Push plugin' + error);
    });
  }

这是我发送推送通知的 php 代码

<?php
    public function androidNotification($title, $message, $registrationIds, $addData) {
        // API access key from Google API's Console
        define( 'API_ACCESS_KEY', 'accesskeyhere' );
        // prep the bundle
        $msg = array
        (
            'message' => $message,
            'title' => $title,
            'addData' => $addData,
            'vibrate' => 1,
            'sound' => 1,
            'largeIcon' => 'large_icon',
            'smallIcon' => 'small_icon'
        );
        $fields = array
        (
            'registration_ids' => $registrationIds,
            'data' => $msg
        );

        $headers = array
        (
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json'
        );
        $ch = curl_init();
        curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
        curl_setopt( $ch,CURLOPT_POST, true );
        curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
        curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
        $result = curl_exec($ch );
        curl_close( $ch );
    }

这里有什么问题?我正在使用离子平台。

我正在搜索论坛,但没有运气。我没有找到任何解决方案。

【问题讨论】:

    标签: php android ionic-framework google-cloud-messaging phonegap-pushplugin


    【解决方案1】:

    您可以参考此thread。尝试改变你的优先级。在page 中声明,将优先级设置为2 可以正常工作。

    【讨论】:

    • 我可以在哪里设置优先级 2?在 php 代码中还是在 app.component.ts 中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多