-2

我試圖開發一個跨平臺的應用程序,它將接收來自Amazon SNS的推送通知。推送通知對於iOS來說工作得很好,但對於Android來說,目前在交叉道路上是這樣。要在Android Manifest中加入GCM推送通知的權限

當Android應用程序集中時,可以查看推送通知。但是,無論我設置什麼變量(如下所示),都會涉及到ti.cloudpush

問題是 - 我無法讓推送通知顯示在通知欄中。

CloudPush.showAppOnTrayClick = true; 
CloudPush.showTrayNotification = true; 
CloudPush.showTrayNotificationsWhenFocused= false; 
CloudPush.singleCallback  = true; 

我想這是關係到我可能要設置IIN的tiapp.xml的Android清單部分權限。我已經包括下面目前使用的權限列表 -

<android xmlns:android="http://schemas.android.com/apk/res/android"> 
    <uses-sdk>14</uses-sdk>&gt; 
    <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

你的實際問題是什麼?具體 – Anand

+0

你有什麼錯誤嗎?如果是,請發佈。 –

+0

對不起,如果我是模糊的。我已經強調了上述問題。請分享你的想法。 – FlyingV

回答

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只是檢查這下面lines.Add,如果不是有:

<modules> 
    <module platform="commonjs">ti.cloud</module> 
    <module platform="android">ti.cloudpush</module> 
</modules> 

這適用於我。希望這可以幫助。

+0

嗨悉達思。感謝您的答覆。我最初使用與你的答案狀態相同的邏輯。但是,應用程序在收到推送通知時會崩潰。由於錯誤是 ** - 無法實例化接收器java.lang.ClassNotFoundException ** 在研究中,我發現以下鏈接幫助我在應用程序被集中時正常工作。 [link] http://stackoverflow.com/questions/9601373/unable-to-instantiate-receiver-java-lang-classnotfoundexception 您的代碼是否在android通知托盤中顯示通知? – FlyingV

+0

您可以請發佈錯誤日誌details.This解決方案適合我。 –

+0

是的這段代碼適用於android通知托盤。只需試一試。我使用3.2.1 GA版本。 –