我正在尝试开发一个跨平台应用程序,它将从Amazon SNS接收推送通知。推送通知对于iOS来说效果很好,但对于Android,我现在正处于十字路口。
当Android应用程序聚焦时,可以查看推送通知。但是,无论与 ti.cloudpush 相关的变量是什么(我在下面列出)。
问题是 - 我无法将推送通知显示在通知托盘中。
CloudPush.showAppOnTrayClick = true;
CloudPush.showTrayNotification = true;
CloudPush.showTrayNotificationsWhenFocused= false;
CloudPush.singleCallback = true;
我想这与Permissions相关,我可能需要在Android Manifest部分的tiapp.xml中设置。我已经包含了以下目前使用的权限列表 -
<android xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk>14</uses-sdk>>
<manifest>
<uses-sdk android:minSdkVersion="14"/>
<permission android:name="com.test.push.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="com.test.push.permission.C2D_MESSAGE"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application>
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND">
<!-- Start receiver on boot -->
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.USER_PRESENT"/>
<category android:name="android.intent.category.HOME"/>
</intent-filter>
<!-- Receive the actual message -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="com.test.push"/>
</intent-filter>
<!-- Receive the registration id -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
<category android:name="com.test.push.permission"/>
</intent-filter>
</receiver>
</application>
</manifest>
</android>
有人可以让我知道我做错了什么/如何做到这一点?
任何与信息/权限相关的链接都将受到高度赞赏。
答案 0 :(得分:0)
试试这段代码:
app.js 类:
/*
Push notifications through device token.
Steps :
1) Retrieve device token
2) Subscribe for receiving notifications
3) Notify
4) Unsubscribe from receiving notifications
*/
Titanium.UI.setBackgroundColor('#000');
var GcmWin = Ti.UI.createWindow({
backgroundColor : '#ccc',
title : 'Android Cloud Push Notification'
});
var CloudPush = require('ti.cloudpush');
CloudPush.debug = true;
CloudPush.enabled = true;
CloudPush.showTrayNotificationsWhenFocused = true;
CloudPush.focusAppOnPush = false;
var deviceToken;
var Cloud = require('ti.cloud');
Cloud.debug = true;
var submit = Ti.UI.createButton({
title : 'Retrieve Device token',
color : '#000',
height : 80,
width : 200,
top : 50
});
var subscribe = Ti.UI.createButton({
title : 'Subscribe',
color : '#000',
height : 80,
width : 200,
top : 150
});
subscribe.addEventListener('click', subscribeToChannel);
GcmWin.add(subscribe);
var notify = Ti.UI.createButton({
title : 'Notify',
color : '#000',
height : 80,
width : 200,
top : 250
});
notify.addEventListener('click', sendTestNotification);
GcmWin.add(notify);
var unsubscribe = Ti.UI.createButton({
title : 'Unsubscribe',
color : '#000',
height : 80,
width : 200,
top : 350
});
unsubscribe.addEventListener('click', unsubscribeToChannel);
GcmWin.add(unsubscribe);
GcmWin.add(submit);
submit.addEventListener('click', function(e) {
CloudPush.retrieveDeviceToken({
success : function deviceTokenSuccess(e) {
alert('Device Token: ' + e.deviceToken);
deviceToken = e.deviceToken;
},
error : function deviceTokenError(e) {
alert('Failed to register for push! ' + e.error);
}
});
});
function subscribeToChannel() {
// Subscribes the device to the 'test' channel
// Specify the push type as either 'android' for Android or 'ios' for iOS
Cloud.PushNotifications.subscribeToken({
device_token : deviceToken,
channel : 'test',
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 sendTestNotification() {
// Sends an 'This is a test.' alert to specified device if its subscribed to the 'test' channel.
Cloud.PushNotifications.notifyTokens({
to_tokens : deviceToken,
channel : 'test',
payload : 'This is a test.'
}, function(e) {
if (e.success) {
alert('Push notification sent');
} else {
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
}
});
}
function unsubscribeToChannel() {
// Unsubscribes the device from the 'test' channel
Cloud.PushNotifications.unsubscribeToken({
device_token : deviceToken,
channel : 'test',
}, function(e) {
if (e.success) {
alert('Unsubscribed');
} else {
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
}
});
}
CloudPush.addEventListener('callback', function(evt) {
//alert(evt);
alert(evt.payload);
});
CloudPush.addEventListener('trayClickLaunchedApp', function(evt) {
Ti.API.info('Tray Click Launched App (app was not running)');
//alert('Tray Click Launched App (app was not running');
});
CloudPush.addEventListener('trayClickFocusedApp', function(evt) {
Ti.API.info('Tray Click Focused App (app was already running)');
//alert('Tray Click Focused App (app was already running)');
});
在 tiapp.xml 中,只需检查以下行。添加它,如果没有:
<modules>
<module platform="commonjs">ti.cloud</module>
<module platform="android">ti.cloudpush</module>
</modules>
这对我有用。希望这会有所帮助。