我正在尝试使用Titanium with Alloy订阅云服务。
我已经配置了Google云消息传递(GCM)API密钥和我的应用的ACM,但是在cload.appcelarator.com推送通知部分中显示:"向您订阅的设备发送推送通知。 您目前有0个iOS客户端, 0个Android客户端订阅推送通知。有什么想法吗?
"这是我测试的代码:
index.js
var CloudPush = require('ti.cloudpush');
CloudPush.retrieveDeviceToken({
success: function deviceTokenSuccess(e) {
// Use this device token with Ti.Cloud.PushNotifications calls
// to subscribe and unsubscribe to push notification channels
Ti.API.info('Device Token: ' + e.deviceToken);
},
error: function deviceTokenError(e) {
alert('Failed to register for push :(! ' + e.error);
}
});
// These events monitor incoming push notifications
CloudPush.addEventListener('callback', function (evt) {
alert(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)');
});
$.index.open();
非常感谢您的任何建议或帮助。
答案 0 :(得分:2)
经过几天的尝试......我已经有了推送通知
index.js
// Require the module
var CloudPush = require('ti.cloudpush');
var deviceToken = null;
// Initialize the module
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("--->" + deviceToken);
subscribeToChannel();
}
function deviceTokenError(e) {
alert('Failed to register for push notifications! ' + e.error);
}
// Process incoming push notifications
CloudPush.addEventListener('callback', function (evt) {
alert("Notification received: " + evt.payload);
});
// For this example to work, you need to get the device token. See the previous section.
// You also need an ACS user account.
// Require in the Cloud module
var Cloud = require("ti.cloud");
function loginUser(){
// Log in to ACS
Cloud.Users.login({
login: 'YOURACSUSER',
password: 'YOURACSPASSWORD'
}, function (e) {
if (e.success) {
alert('Login successful');
} else {
alert('Error:\n' +
((e.error && e.message) || JSON.stringify(e)));
}
});
}
function subscribeToChannel(){
// Subscribe the user and device to the 'test' channel
// Specify the push type as either 'android' for Android or 'ios' for iOS
// Check if logged in:
Cloud.PushNotifications.subscribe({
channel: 'test',
//device_token: 'APA91bHRjGoZLCYKwn-XcCtNLETuf-KRKfT4sMgVE4KgXQgInYfZuYTNrZC7FUMugLs0idzzqtLytrvVJjVzYBzQoc7Q81hEerq0O2vww_tV8mACuUfAi0JRvs7LoufnQZpYLZrb_1rlUsIOEMsPxDs9b_pIRJF5rw',
device_token:deviceToken,
type: Ti.Platform.name == 'android' ? 'android' : 'ios'
}, function (e) {
if (e.success) {
alert('Subscribed');
} else {
alert('Error:\n' +
((e.error && e.message) || JSON.stringify(e)));
}
});
}
function unsubscribeToChannel (){
// Unsubscribes the user and device from the 'test' channel
Cloud.PushNotifications.unsubscribe({
channel: 'test',
device_token: deviceToken
}, function (e) {
if (e.success) {
alert('Unsubscribed');
} else {
alert('Error:\n' +
((e.error && e.message) || JSON.stringify(e)));
}
});
}
loginUser();
ti.app.xml中的
<?xml version="1.0" encoding="UTF-8"?>
<ti:app xmlns:ti="http://ti.appcelerator.org">
<property name="acs-api-key-production" type="string">XCLR6QnXrxQrDIx7OtI4DiHPr32qKTx3</property>
<property name="acs-api-key-development" type="string">Sk77CE9ZJeA6eXohelrD9UKst7Ktgeph</property>
<property name="acs-api-key" type="string">GOOGLE API</property> <!--API DE GOOGLE -->
<id>com.creativa.petcare.ro</id>
<name>PetCare</name>
<version>1.0</version>
<publisher>ricardo.orellana</publisher>
<url>http://</url>
<description/>
<copyright>2015 by ricardo.orellana</copyright>
<icon>appicon.png</icon>
<fullscreen>false</fullscreen>
<navbar-hidden>false</navbar-hidden>
<analytics>true</analytics>
<guid>254dae59-8688-45b6-9f82-331b6f378a85</guid>
<property name="ti.ui.defaultunit" type="string">dp</property>
<ios>
<plist>
<dict>
<key>UISupportedInterfaceOrientations~iphone</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIRequiresPersistentWiFi</key>
<false/>
<key>UIPrerenderedIcon</key>
<false/>
<key>UIStatusBarHidden</key>
<false/>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleDefault</string>
</dict>
</plist>
</ios>
<android xmlns:android="http://schemas.android.com/apk/res/android"/>
<mobileweb>
<precache/>
<splash>
<enabled>true</enabled>
<inline-css-images>true</inline-css-images>
</splash>
<theme>default</theme>
</mobileweb>
<modules>
<module platform="commonjs">ti.cloud</module>
<module platform="android">ti.cloudpush</module>
</modules>
<deployment-targets>
<target device="iphone">false</target>
<target device="ipad">false</target>
<target device="android">true</target>
<target device="blackberry">false</target>
<target device="mobileweb">true</target>
</deployment-targets>
<sdk-version>3.5.0.GA</sdk-version>
<plugins>
<plugin version="1.0">ti.alloy</plugin>
</plugins>
</ti:app>
如果这个代码对任何有同样怀疑的人都有帮助,那将是一种乐趣......