钛合金推送通知订阅但听众没有开火

时间:2016-03-01 05:54:27

标签: android google-cloud-messaging titanium titanium-mobile titanium-alloy

我需要创建一个推送通知监听器,我的代码在下面,它运行完美,直到订阅和仪表板确认它已成功订阅,但接收器出现问题。

Alloy.Globals.ServiceAddress = "my service address";
Ti.API.info("1");
var CloudPush = require('ti.cloudpush');
var Cloud = require("ti.cloud");
var deviceToken = null;

loginUser(); // flow starts from here
function loginUser() {
  Cloud.Users.login({
    login: 'User',
    password: 'Password'
  }, function(e) {
    if (e.success) {
      var user = e.users[0];
      //    alert("Loggin successfully");
      getDeviceToken();
    } else {
      alert("Error2*** :" + e.message);
      //Ti.API.info('error ')
    }
  });
}

function getDeviceToken() {

  if (Ti.Platform.Android) {
    CloudPush.debug = true;
    CloudPush.focusAppOnPush = false;
    CloudPush.enabled = true;
    CloudPush.showAppOnTrayClick = true;
    CloudPush.showTrayNotification = true;
    CloudPush.singleCallback = true;
    CloudPush.singleCallback = true;
    CloudPush.retrieveDeviceToken({
      success: deviceTokenSuccess,
      error: deviceTokenError
    });
  }
}

// Enable push notifications for this device
// Save the device token for subsequent API calls
function deviceTokenSuccess(e) {
  deviceToken = e.deviceToken;
  alert(e.deviceToken);
  subscribeToChannel(deviceToken);
}

function deviceTokenError(e) {
  alert('Failed to register for push notifications! ' + e.error);
}

// Process incoming push notifications
function subscribeToChannel(deviceToken) {

  // Subscribes the device to the 'news_alerts' channel
  // Specify the push type as either 'android' for Android or 'ios' for iOS
  Cloud.PushNotifications.subscribeToken({
    device_token: deviceToken,
    channel: 'Vision', //'You_App',
    type: Ti.Platform.name == 'android' ? 'android' : 'ios'
  }, function(e) {
    if (e.success) {
      alert('Subscribed');
      SendNotification(deviceToken);
    } else {
      // indicator.visible = false;
      alert('Subscribe Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
    }
  });
}

function SendNotification(to_deviceToken) {
  alert('sendnot');
  Cloud.PushNotifications.notifyTokens({
    channel: 'My Cannel',
    to_tokens: to_deviceToken,
    payload: 'Welcome to push notifications'
  }, function(e) {
    if (e.success) {
      alert('Success Send');
    } else {
      alert('Send Error:\n' +
        ((e.error && e.message) || JSON.stringify(e)));
    }
  });
}

当我添加事件监听器以推送通知时,它甚至没有启动,客户端说它已连接。

<!-- language: lang-js -->
CloudPush.addEventListener('callback', function (evt) {
    alert('rec');
    alert("Notification received: " + evt.payload);
});
CloudPush.addEventListener('trayClickLaunchedApp', function (evt) {
    Ti.API.info('Tray Click Launched App (app was not running)');
});
CloudPush.addEventListener('trayClickFocusedApp', function (evt) {
    Ti.API.info('Tray Click Focused App (app was already running)');
});

PS。我已搜索并已阅读相关主题Like this one

我附上日志以澄清订阅和通知发送是否正常 Push notification LogConnected Clients

1 个答案:

答案 0 :(得分:0)

解决 我正在使用带有CloudPush模块版本3.2.1的钛api级别4.1.1.v20151203143424进行编译 我使用cloudpush模块版本3.4.1迁移到钛6.0.0.v20160202173231 所有设置见this pic