Cordova Push Plugin: onNotificationGMC is not fired and cannot obtain regID

前端 未结 3 1074
情书的邮戳
情书的邮戳 2021-01-13 06:01

Hello everyone I\'m developing a cordova Hybrid app that requires the Push Notification Service of Android and iOS to work and so I\'ve installed the cordova plugin \"PushPl

3条回答
  •  北海茫月
    2021-01-13 06:58

    In the ionic-framework you have a ready plugin: http://ngcordova.com/docs/plugins/pushNotifications/

    here is an example for a working code for android devices:

    module.run(function($cordovaPush) {
    
    var androidConfig = {
        "senderID": "replace_with_sender_id",
        "ecb": "replace_with_the_name_of_function that will return you the regid"
    };
    
    document.addEventListener("deviceready", function(){
    $cordovaPush.register(config).then(function(result) {
      // Success
    }, function(err) {
      // Error
    })
    
    window.function replace_with_ecb(notification) { //notification.regid
      switch(notification.event) {
        case 'registered':
          if (notification.regid.length > 0 ) {
            alert('registration 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;
      }
    };
    
    }, false);
    });
    

    this code only works a real device(not an emulator)

提交回复
热议问题