2

與我的PhoneGap 3 iOS應用程序一起使用PushPlugin,我有推送通知主要工作,但我發送通知到自定義有效載荷的APNS。這是什麼樣子:PhoneGap PushPlugin - 訪問iOS APNS負載?

$body = array(
    'customID' => $customID 
); 
$body['aps'] = array(
    'alert' => $message, 
    'sound' => 'default' 
); 

從我讀過,這是應該由APNS(scroll down to the bottom)的支持,但PushPlugin's documentation不會在所有iOS的剷球非常好。我想要的是當我的應用程序通過點擊通知來打開時,能夠將customID作爲JS變量訪問。當應用程序自行啓動時,我不希望發生任何特別的事情。此外,重要的是用戶通知哪個通知 - customID是獨特且重要的。我只是很困惑,因爲我找不到任何人在討論使用PushPlugin處理這些定製APNS有效載荷。

回答

1

我從來沒有使用PushPlugin,但看着他們的代碼,我可以指出你在正確的方向。

AppDelegate中,他們將包含通知有效內容的字典傳遞給PushPlugin對象的實例,並調用該實例的notificationReceived方法。

notificationReceived of PushPlugin從該字典構造一個JSON字符串並將其傳遞給處理通知的JS回調方法。

下面是從PushPlugin文檔回調方法的例子:

function onNotificationAPN (event) { 
    if (event.alert) 
    { 
     navigator.notification.alert(event.alert); 
    } 

    if (event.sound) 
    { 
     var snd = new Media(event.sound); 
     snd.play(); 
    } 

    if (event.badge) 
    { 
     pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge); 
    } 
} 

我不是在JavaScript很流利,但event應該包含JSON字符串與通知有效載荷的所有數據,包括您的自定義屬性。你應該可以在那裏訪問它。

+0

現在,我不得不放棄這一點,但我不得不認爲這是正確的... – tylerl

2

添加自定義變量,以推送通知有效載荷的正確的方法是不要將它添加到APS命名空間(這一​​次是由蘋果保留),但分開,如:

$body['aps'] = array(
    'alert' => $message, 
    'sound' => 'default' 
); 

$body['payload'] = array(
    'customID' => $customID 
); 

內的PhoneGap ,您可以將您的通知回調(如onNotificationAPN)內,像這樣再閱讀:

function onNotificationAPN (e) { 
    if (e.customID) 
    { 
     navigator.notification.alert("CustomID: " + e.customID); 
    } 
+0

'e'在哪裏定義? – slashwhatever

+0

我在代碼段中輸入了一個錯字 - 它指的是參數。更正它 – Mobiletainment