2016-08-01 14 views
1

當我的應用程序處於前景時,我正在使用Cordova和插件phonegap-plugin-push, 我沒有看到通知警報。 我需要做什麼? (我知道在Android平臺上執行此操作的方式(在PushNotification的init方法中設置爲「forceShow」:true),但這僅適用於Android) 預先感謝您的回覆。cordova iOS通知不能在前臺工作

+0

謝謝所有!我使用cordova插件的push.finish()方法解決了問題,現在顯示了橫幅,但是當它消失時,通知不會存儲在設備讀取通知列表中,這是一個問題!任何想法來解決它? – Denise

回答

0

丹尼斯,

通知不能在前臺工作。它只能在後臺運行。在前臺處理它需要在委託方法中實現邏輯。 以下是在前臺顯示通知的Objective C代碼片段。

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 
    application.applicationIconBadgeNumber = 0; 
    //self.textView.text = [userInfo description]; 
    // We can determine whether an application is launched as a result of the user tapping the action 
    // button or whether the notification was delivered to the already-running application by examining 
    // the application state. 

    if (application.applicationState == UIApplicationStateActive) 
    { 
     // Nothing to do if applicationState is Inactive, the iOS already displayed an alert view. 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Did receive a Remote Notification" message:[NSString stringWithFormat:@"Your App name received this notification while it was running:\n%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 

     [alertView show]; 
    }  
} 

它會幫助你解決你的問題。

+0

感謝您的回覆!但我使用科爾多瓦,你知道如果是在cordova修改javascript的方式嗎? – Denise

+0

它的安全方式@Denise。您可以自由使用它,並且不會導致任何應用程序拒絕。歡呼聲 – Sabby

0

在iOS中,如果您的應用程序處於前景中,則不會顯示推送通知警報。要實現它,您應該在推送通知接收委託中編寫警報代碼,前提是您的應用程序處於前臺。要知道iOS中的應用程序狀態,這是代碼。

+(BOOL) runningInForeground 
{ 
UIApplicationState state = [UIApplication sharedApplication].applicationState; 

return state == UIApplicationStateActive; 

} 
+0

@iamgam感謝您的回覆!但我使用科爾多瓦,你知道如果是在cordova修改javascript的方法嗎?或者只能使用本機代碼? – Denise

+0

@Denise,請閱讀此[鏈接](https://github.com/phonegap-build/PushPlugin)'// iOS function onNotificationAPN(event){ if(event.alert) { navigator.notification。警報(event.alert); (event.sound) var snd = new Media(event.sound); snd.play(); (event.badge) { } pushNotification.setApplicationIconBadgeNumber(successHandler,errorHandler,event.badge); } }' –

+0

如果這可以幫助你,請投票並接受答案。 –

1

如果妳不想碰iOS原生的東西,那麼你就可以顯示自己的custmize HTML/CSS橫幅(如iOS的推巴納爾)在下面的方法:

push.on('notification', function(data) { 
    If(platform == "iOS" && data.additionalData.foreground){ 
    // Show your Baner here 
// U can also define call back on clicking of that banner 
//Like navigation to respective page or close that banner 
}); 
+0

謝謝!我用類似的方法使用push.finish()方法解決了問題,現在顯示了橫幅,但是當它消失時,通知不會存儲在設備讀取通知列表中,這是一個問題!任何想法來解決它? – Denise

+0

我不理解你的新問題。但我想提供一些可能對您有所幫助的信息。 1.首先,該橫幅將成爲應用程序Web視圖的一部分,因此它絕不會存儲在通知中心的本機通知列表中。 2.當橫幅將消失,那麼它將不再是你的應用程序的一部分。 3.您也可以使用** cordova-plugin-local-notifications **來顯示本地通知,而不是橫幅 –