【问题标题】:Cannot receive notifications in ionic framework GCM无法在离子框架 GCM 中接收通知
【发布时间】:2015-08-08 08:45:59
【问题描述】:

我正在使用带有离子框架的 ngcordova push 插件。 我的设备正在GCM 中注册。我也得到了注册ID。我能够将regid 存储在用户表中。到目前为止一切正常。但是当我从我的 php 服务器发送通知时,我无法在手机中收到通知。我得到的 gcm 对 php 代码的响应是:

  {"multicast_id":5214502018855543648,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1439022666580761%156ac804f9fd7ecd"}]}

我的 JavaScript 代码是。

.run(function($ionicPlatform,ngFB,$state,$cordovaPush,$rootScope) {
$ionicPlatform.ready(function() {


// ------------------------------ GCM NG-CORDOVA PUSH-----------------------------------
var androidConfig = {
    "senderID": "XXXXXXXXXX"
  };
 $cordovaPush.register(androidConfig).then(function(result) {
      // Success
      alert(result);
    }, function(err) {
      // Error
      alert(err);
    });

    $rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
      switch(notification.event) {
        case 'registered':
          if (notification.regid.length > 0 ) {
            alert('registration ID = ' + notification.regid);
            $rootScope.reg_id = notification.regid;
          }
          break;

        case 'message':
          // this is the actual push notification. its format depends on the data model from the push server
          alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);
          break;

        case 'error':
          alert('GCM error = ' + notification.msg);
          break;

        default:
          alert('An unknown GCM event has occurred');
          break;
      }
    });



    // WARNING: dangerous to unregister (results in loss of tokenID)
   // $cordovaPush.unregister(options).then(function(result) {
      // Success!
    //}, function(err) {
      // Error
    //})


  });


})

我的php代码是:

<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR-API-ACCESS-KEY-GOES-HERE' );
$registrationIds = array( $_GET['id'] );
// prep the bundle
$msg = array
(
    'message'   => 'here is a message. message',
    'title'     => 'This is a title. title',
    'subtitle'  => 'This is a subtitle. subtitle',
    'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
    '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 );
echo $result;

我不明白我错在哪里。请帮帮我。

谢谢

【问题讨论】:

    标签: android cordova push-notification google-cloud-messaging ionic-framework


    【解决方案1】:

    我曾经遇到过同样的问题,我建议您使用下面的链接创建您的第一个应用程序,并让第二个 regid 确保设备正确、保持良好并且应用程序的密钥正确。尝试使用相同的浏览器发送通知,有时时间会有所不同。

    add android app

    【讨论】:

    • 现在我收到这个警报 `case 'message': alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);休息; ` message = undefined msgCount = undefined
    • 现在我在 ionic 中收到消息,我可以在警报中显示它。但是如何在通知托盘中显示它。
    • 不显示通知?很奇怪,它应该出现。尝试从您的服务器的 URL 发送,验证 reg id 是否正确保存,并在案例记录时从应用程序发送警报。编辑是我的答案。您将 reg id 的值保存在哪里?在您的数据库中还是只有 rootScope?
    • 最后我得到了它的工作谢谢我拼错了消息字段
    • 太棒了!如果您标记它对我的回答有帮助,您好!
    【解决方案2】:

    确保将 $cordovaPush.register() 和 $rootScope.$on() 函数包装在 document.addEventListener("deviceready", function() {} 像这样

      document.addEventListener("deviceready", function() {
      console.log("insode ");
        $cordovaPush.register(androidConfig).then(function(result) {
            console.log(result);
            // Success
        }, function(err) {
            console.log(err);
            // Error
        })
        $rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
            console.log(event);
            console.log(notification);
            switch (notification.event) {
                case 'registered':
                    if (notification.regid.length > 0) {
                        //alert('registration ID = ' + notification.regid);
                    }
                    $http({
                      url: "http://192.168.1.101/saveregid.php",
                      method : "POST",
                      data : {
                        id : notification.regid
                      }
                      }).success(function(){
                      alert("Id is saved");
                      });
    
                    break;
                case 'message':
                    // this is the actual push notification. its format depends on the data model from the push server
                    $scope.notification = notification;
                    //alert('message = ' + notification + ' msgCount = ' + notification.msgcnt);
                    break;
                case 'error':
                    alert('GCM error = ' + notification.msg);
                    break;
                default:
                    alert('An unknown GCM event has occurred');
                    break;
            }
        });
    }, false);
    

    为了获得正确的注册 ID,我在 http://192.168.1.101/saveregid.php 制作了一个简单的脚本,该脚本在其发布请求中获取注册 ID,并将其存储在我可以访问的服务器文件中。从服务器文件中获取准确的 reg id,然后尝试发送通知。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      • 2014-06-04
      相关资源
      最近更新 更多