기술 글
Push Notification URL 전달하기
레일즈 쪽 푸시 노티 코드입니다. def send_push_message...
- 표시 날짜
- 읽는 시간
- 1 min read
레일즈 쪽 푸시 노티 코드입니다.
def send_push_message(body, title = nil, badge_count = 1, url: nil)
return if device_token.blank?
title ||= 'APP TITLE'
fcm = FCM.new(Rails.application.credentials.fcm)
registration_ids = [device_token]
options = {
data: { title: title, body: body, url: url },
collapse_key: "message_#{Time.zone.now.change(min: 0)}"
}
options = options.merge(notification: { title: title, body: body, url: url, badge: badge_count }) if device_type == 'ios'
res = fcm.send(registration_ids, options)
end
전달하고 싶은 url 을 push 메세지 url 부분에 함께 전달합니다.
IOS 코드입니다.
기존에 이미 푸시 노티가 동작하는 상태에서 아래코드 추가 시 동작합니다.
## viewController.swift
---
NotificationCenter.default.addObserver(forName: .UIApplicationDidBecomeActive, object: nil, queue: nil) { [weak self] n in
let url = Utils.getUserValue("notificationUrlId")
if let temp = url {
self?.webView.load(URLRequest(url: URL(string: "\(Constant.baseURL)?redirect_to=\(temp)&platform=ios")!))
UserDefaults.standard.removeObject(forKey: "notificationUrlId")
UserDefaults.standard.synchronize()
return
}
}
---
## AppDelegate.swift
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
let url = userInfo["url"] as? String
if let temp = url{
print("@@@ url-id 저장: \(temp)")
Utils.setUserValue("notificationUrlId", url)
UserDefaults.standard.synchronize()
}
Android쪽 코드입니다.
protected void onCreate(Bundle savedInstanceState) {
...
String url = checkMessage(getIntent());
if(url == null) {
webview.loadUrl(getResources().getString(R.string.url));
}else{
Log.d("URL", url);
webview.loadUrl(getResources().getString(R.string.domain) +"/?redirect_to=" + url + "&platform=android");
}
...
}
private String checkMessage(Intent intent) {
String url = null;
if(intent != null){
Bundle bundle = intent.getExtras();
if(bundle != null){
url = bundle.getString("url");
}
}
return url;
}
앱에서 넘겨주는 redirect_to 파라미터를 가지고 프론트에서 해당 페이지로 이동시켜주게 됩니다.