【问题标题】:ionic 2 push notification not received when app is closed or in background应用程序关闭或在后台时未收到 ionic 2 推送通知
【发布时间】:2018-07-03 23:33:11
【问题描述】:

应用打开时:推送通知就可以了

当应用程序在后台时:推送通知振动和声音但不显示在通知栏上

应用关闭时:未收到通知

这是我的推送通知初始化

  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);
    });
  }

这是我的后端发件人:

// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'accesskeyhere' );
// device ids
$registrationIds = array('mydeviceidhere');
// prep the bundle
$msg = array
(
    'message' => "This is a Test Notification",
    'title' => "This is a Test Title",
    'addData' => "This is Test Additional Data",
    '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 );

我做错了什么?还是我的代码中缺少某些东西?

当应用程序关闭/在后台使用 ionic 2 时如何唤醒 GCM 服务? 这里有人以前有同样的问题并修复了吗?请分享你的答案。谢谢你

【问题讨论】:

    标签: ionic2 phonegap-pushplugin


    【解决方案1】:
    猜你喜欢
    • 2015-09-21
    • 1970-01-01
    • 2020-12-23
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 2015-04-12
    相关资源
    最近更新 更多