了解$ window.onNotificationGCM的流程流程

时间:2015-09-13 01:44:13

标签: phonegap-pushplugin

看看这段代码,我想知道触发onNotificationGCM的是什么?它是在应用程序注册时触发的,但是什么时候再次触发,比如何时我想向用户发送消息?我有一个聊天应用程序,我想在聊天进来时发送消息。所以我明白我注册了设备但是这个代码需要再次运行,我认为,新事件。我只需要了解零件流程和零件代码。

// handle GCM notifications for Android
$window.onNotificationGCM = function (event) {
switch (event.event) {
  case 'registered':
    if (event.regid.length > 0) {
      // Your GCM push server needs to know the regID before it can push to this device
      // here is where you might want to send it the regID for later use.
      var device_token = event.regid;
      RequestsService.register(device_token).then(function(response){
          alert('registered!');
        });
      //send device reg id to server

    }
    break;

  case 'message':
      // if this flag is set, this notification happened while we were in the foreground.
      // you might want to play a sound to get the user's attention, throw up a dialog, etc.
      if (event.foreground) {
            console.log('INLINE NOTIFICATION');
            var my_media = new Media("/android_asset/www/" + event.soundname);
            my_media.play();
      } else {
        if (event.coldstart) {
            console.log('COLDSTART NOTIFICATION');
        } else {
            console.log('BACKGROUND NOTIFICATION');
        }
      }

      navigator.notification.alert(event.payload.message);
      console.log('MESSAGE -> MSG: ' + event.payload.message);
      //Only works for GCM
      console.log('MESSAGE -> MSGCNT: ' + event.payload.msgcnt);
      //Only works on Amazon Fire OS
      console.log('MESSAGE -> TIME: ' + event.payload.timeStamp);
      break;

  case 'error':
      console.log('ERROR -> MSG:' + event.msg);
      break;

  default:
      console.log('EVENT -> Unknown, an event was received and we do not know what it is');
      break;
}

};

1 个答案:

答案 0 :(得分:0)

看一下这个例子:

case 'message':
    /*
    if (e.foreground) {
    window.alert("Message recieved");
    }
    else {
    if (e.coldstart) {
    window.alert("Coldstart Notification");
    } else {
    window.alert("Background Notification");
    }
    }
    window.alert("Notification message: " + e.payload.message
               + "\n\n Time: " + e.payload.conversation);
    */

    var data = e.payload;
    if (data.conversation){
        window.history.replaceState(null, '', '#/chats/');
        ProjectName.conversation.load(data.conversation);
    }
    if (data.product){
        window.history.replaceState(null, '', '#/product/' + data.product);
    }
    break;
case 'error':
    // window.alert("Notification error: " + e.msg);
    break;
default:
    // window.alert("Notification - Unknown event");
    break;
}

e.payload包含您发送到应用程序的所有数据,包括message。您可以在case 'message'

中访问其他变量