[英]android push notification in ionic
我正在使用ionic框架開發移動應用程序。我已經成功實現了推送通知。當我單擊通知時,我希望它應該顯示在特定頁面上,我該怎么做? 在客戶端:
var user = $ionicUser.get();
if(!user.user_id) {
// Set your user_id here, or generate a random one.
user.user_id = $ionicUser.generateGUID();
};
// Identify your user with the Ionic User Service
$ionicUser.identify(user).then(function(){
//$scope.identified = true;
//alert('User ID ' + user.user_id);
//alert("here");
$ionicPush.register({
canShowAlert: true, //Can pushes show an alert on your screen?
canSetBadge: true, //Can pushes update app icon badges?
canPlaySound: true, //Can notifications play a sound?
canRunActionsOnWake: true, //Can run actions outside the app,
onNotification: function(notification) {
// Handle new push notifications here
// console.log(notification);
return true;
}
});
});
});
})
.config(['$ionicAppProvider', function($ionicAppProvider) {
$ionicAppProvider.identify({
app_id: 'xxxxxxxxxxxxx',
api_key: 'xxxxxxxxxxxxxx',
dev_push: false
});
}])
在服務器端,我使用了php:
$deviceToken = 'APA91bHKwBKrIjmmZ9lke97fl_GbOQ9iRRo-S2sNnZp085hPqVaTHMOd0wqhYFF1PtrtOzFrETX7ZNIkU0JDhC49Tby_AEFIQFRX99B0QpZd7xdiTqsP6sZ08P-8ESdJAie5AJNxhW89nQ7S9evZNcAc9tsJaG91Xw';
$registrationIds = explode(",",$deviceToken);
//$registrationIds = array($deviceToken);
// prep the bundle
$msg = array
(
'notId' => time(),
'message' => 'Technovault is awesome!!!',
'title' => 'Title',
'smallIcon' =>'icon'
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . 'xxxxxxxxxx',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
}
else {
echo "Error";
}
我幾天前解決了類似情況。 我有一個帶有文章列表和文章詳細信息的應用程序。 當有新文章時,服務器將發送通知,而不是在播放負載中包含文章ID。
event i can read this localStorage item and change the state of the app and show the article controller view for this article id. 當應用程序收到通知時,我將其保存在localStorage中,並且當用戶單擊通知時,通過事件,我可以讀取此localStorage項目並更改應用程序的狀態,並顯示此文章ID的文章控制器視圖。
library, i used the Cordova because i have a custom push notification server. 我沒有使用庫, 使用了Cordova 因為我有一個自定義的推送通知服務器。
應用收到通知后:
$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
if (ionic.Platform.isAndroid() && notification.event == 'message') {
var newsid=notification.payload.newsid;
if (typeof newsid != 'undefined') {
// save the newsid to read it later in the resume event
$window.localStorage.setItem('goafterpush',newsid);
}
}
});
event: 和事件:
$ionicPlatform.on("resume",function(event){
// read the newsid saved previously when received the notification
var goafterpush=$window.localStorage.getItem('goafterpush');
if (goafterpush) {
$window.localStorage.removeItem('goafterpush');
$state.go('app.newsdetail',{id:goafterpush});
}
});
希望這段代碼對您有所幫助。
我已經為Android和iOS工作,在python中,我已經使用狀態名稱和Id參數設置了有效負載
payload = {"$state":"state.name", "$stateParams": "{\"id\": \""+time()+"\"}"}
然后我將其包含在所有其他消息中
"payload": payload
這應該在$ msg數組內發生,僅在用戶單擊通知時您的應用程序打開並使用提供的參數進入定義的狀態。
您在客戶端收到的notification
具有關聯的參數foreground
。 當您通過單擊通知進入應用程序時,它的值為false
。 因此,這是執行特定操作的方法:
onNotification: function(notification) {
if(notification.foreground == false){
//add your logic here to navigate to other page
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.