Php FCM iOS Receive notification in background - php

I am writing a general FCM function in PHP to send remote notifications to both Android and IOS devices.
In IOS I get notifications in the foreground but when App is in the background, I don't get any notification.
I tried several methods from the net and nothing helped me.
Kindly help me to set the server side properly
Try 1 :
$feildAryVar = array(
'content_available' => true,
'registration_ids' => $FcmUidVar,
'data' => array("MsgKey"=>json_encode($msgToSendVar)),
'notification' => array(
"title"=>"Fcm TtlMsg",
"body"=>"Fcm Dtl Msg",
"content_available"=> true)
);
Try 2 :
$feildAryVar = array(
'registration_ids' => $FcmUidVar,
'data' => array("MsgKey"=>json_encode($msgToSendVar)),
'notification' => array(
"title"=>"Fcm Title Msg",
"body"=>"Fcm Detail Msg",
"content_available"=> true)
);
Ios App Deligate :
func application(appPsgVar: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
{
FIRMessaging.messaging().appDidReceiveMessage(userInfo)
}
func application(AppPsgVar: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void)
{
handler(UIBackgroundFetchResult.NewData)
FIRMessaging.messaging().appDidReceiveMessage(userInfo)
}
#available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
{
print("userNotificationCenter : Forground")
completionHandler([.Alert,.Sound,.Badge])
}
#available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void)
{
//Code Todo After User presess notification
}
func applicationReceivedRemoteMessage(remoteMessage: FIRMessagingRemoteMessage)
{
let fcmNfnVar = remoteMessage.appData
print("Received Notification : \(fcmNfnVar)")
}

Related

Problem Uploading Images to S3 Bucket Using AlamoFire and Presigned URL

My approach for uploading to S3 for my app has been to send a get request to my PHP backend to generate a presigned URL. I know the backend is set up correctly because running the following command successfully uploads an image to the S3 bucket:
curl -v -H "Content-Type: image/jpeg" -T ./test.jpeg '<presignedURL>'
However, I am running into issues when trying to upload the image in Swift. Here is my current implementation (please ignore the garbage, hard-coded, non error checking):
Backend
<?php
require '../vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
$response = array();
$client = S3Client::factory(array(
'profile' => 'default',
'version' => 'latest',
'region' => 'us-east-2',
'signature' => 'v4'
));
$command = $client->getCommand('PutObject', array(
'Bucket' => 'test',
'Key' => 'test.jpeg',
'ContentType' => 'image/jpeg',
'Body' => ''
));
$signedUrl = $command->createPresignedUrl('+5 minutes');
$response['error'] = false;
$response['url'] = $signedUrl;
echo json_encode($response);
Swift Code
import Foundation
import Alamofire
let getTokenURL = "http://192.168.1.59:8000/v1/upload.php"
func submitImage(image: UIImage, completion: #escaping (NSDictionary) -> Void) {
AF.request(getTokenURL, method: .get).responseJSON { response in
switch response.result {
case.success(let value):
let jsonData = value as! NSDictionary
let url = jsonData.value(forKey: "url") as! String
performUpload(image: image, postURL: url)
case.failure(_):
let error_msg: NSDictionary = [
"error" : true,
"message" : "Unknown error occurred. Please try again",
]
//completion(error_msg)
}
}
}
func performUpload(image: UIImage, postURL: String) {
let imageData = image.jpegData(compressionQuality: 0.50)!
AF.upload(imageData, to: postURL, headers: ["Content-Type":"image/jpeg"]) //likely the culprit line
}
Currently the URL is returned from the get request in submitImage(), and performUpload() is called, making the culprit (likely) the very last lime of my Swift code segment. I'm having trouble figuring out what I should do while reading the documentation, and most guides on this subject are old and outdated because AlamoFire has changed their syntax. Any help would be greatly appreciated. Thank you!
Edit:
I have tweaked the performUpload() function. It now uploads the data to the s3 bucket, however the image is not able to be opened. I suspect this is because of an incorrect header in the request. From debugging I can tell the Content-Type header is "multipart/form-data" no matter what, so I'm not sure if this approach is viable:
struct HTTPBinResponse: Decodable { let url: String }
func performUpload(image: UIImage, postURL: String) {
let imageData = image.jpegData(compressionQuality: 0.50)!
AF.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imageData, withName: "file", mimeType: "image/jpeg")
}, to: postURL, method: .put, headers: ["Content-Type":"image/jpeg"]).responseDecodable(of: HTTPBinResponse.self) { response in
debugPrint(response)
}
}
For future readers, the key here is to add method: .put! Everything else is fine in this question.
Also I found that you have to use empty content-type headers. S3 is weird.
AF.upload(imageData, to: postURL, method: .put, headers: ["Content-Type": ""])

Swift: FCM notification not received

I use FCM for notification.
In firebase console i can send notification, but by PHP code not received and cant custom it
PHP code :
$data['TITLE'] = $title;
$data['TEXT'] = $text;
$fcmFields = array('to'=>$to,
'data'=>$data,
'priority'=>'high',
'time_to_live'=>$time);
$headers = array('Authorization: key='.API_ACCESS_KEY,'Content-Type: application/json');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'https://fcm.googleapis.com/fcm/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($fcmFields));
$result = curl_exec($ch);
curl_close($ch);
return $result;
Note: by this php code i can send notification android devices
Swift Code :
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
print(userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Unable to register for remote notifications: \(error.localizedDescription)")
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("APNs token retrieved: \(deviceToken)")
}
}
#available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
print(userInfo)
completionHandler([])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: #escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
print(userInfo)
completionHandler()
}
}
extension AppDelegate : MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Received data message: \(remoteMessage.appData)")
}
}
1- I can send notification by google console and received but not prints anything in my app log
2- I can send notification to android devices by php codes but i cant send for ios devices
3- didReceiveRemoteNotification completely not work
Can you help me ?
I add 'notification' in PHP code and fixed
$fcmFields = array('to'=>$to,
'data'=>$data,
'notification'=>array('title'=>'x','body'=>'y'),
'priority'=>'high',
'time_to_live'=>$time);

Push Notifications in iOS using Firebase and PHP

I'm very new to development and couldn't find much end to end support on this topic. I did what I could using firebase help page.
I'm unable to receive notifications on my ios devices but FCM is working perfectly on android.
Here is my appdelegate from xcode
import UIKit
import Firebase
import FirebaseMessaging
import UserNotifications
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.applicationIconBadgeNumber = 0
// Override point for customization after application launch.
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
// For iOS 10 data message (sent via FCM)
//FIRMessaging.messaging().remoteMessageDelegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
UIApplication.shared.registerForRemoteNotifications()
}
application.registerForRemoteNotifications()
FirebaseApp.configure()
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token1 = Messaging.messaging().fcmToken
print("FCM token: \(token1 ?? "")")
var request = URLRequest(url: URL(string: "http://www.myurl.com/register.php")!)
request.httpMethod = "POST"
let postString = "Token="+token1!
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
}
task.resume()
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Registration failed!")
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
#available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (_ options: UNNotificationPresentationOptions) -> Void) {
// custom code to handle push while app is in the foreground
print("Handle push from foreground\(notification.request.content.userInfo)")
let dict = notification.request.content.userInfo["aps"] as! NSDictionary
let d : [String : Any] = dict["alert"] as! [String : Any]
let body : String = d["body"] as! String
let title : String = d["title"] as! String
// print("Title:\("FOSG NOTIFICATION") + body:\(body)")
self.showAlertAppDelegate(title: "Federation Of Safety Glass",message:body,buttonTitle:"ok",window:self.window!)
}
#available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
// if you set a member variable in didReceiveRemoteNotification, you will know if this is from closed or background
print("Handle push from background or closed\(response.notification.request.content.userInfo)")
}
func showAlertAppDelegate(title: String,message : String,buttonTitle: String,window: UIWindow){
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: buttonTitle, style: UIAlertActionStyle.default, handler: nil))
window.rootViewController?.present(alert, animated: false, completion: nil)
}
// Firebase ended here
}
and this is my code from server end on php
$tokens = array(); $mess = '';
// queries from db to set the values of variables.
function send_notification ($tokens, $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = **key ',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
$message = array(
"body" => $mess,
"message" => $mess,
"title" => "FOSG NOTIFICATION",
"sound" => 1,
"vibrate" => 1,
"badge" => 1,
);
$t = implode('',$tokens);
if(t != '') $message_status = send_notification($tokens, $message);
Please help me learn and resolve my issue
Just changed the field names before sending the json
change the following :
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
to
$fields = array(
'registration_ids' => $tokens,
'notification' => $message,
'priority' => 'high'
);

IOS push notifications not received

I configured my app:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let pushSettings: UIUserNotificationSettings = UIUserNotificationSettings(
forTypes:[.Alert, .Badge, .Sound],
categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(pushSettings)
UIApplication.sharedApplication().registerForRemoteNotifications()
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
print("My token is: \(deviceToken)");
}
// Failed to register for Push
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("Failed to get token; error: %#", error) //Log an error for debugging purposes, user doesn't need to know
}
Copy my device token, to my python script:
import time
from apns import APNs, Frame, Payload
apns = APNs(use_sandbox=True, cert_file='NodesDownCert.pem', key_file='NodesDownKey.pem')
# Send a notification
token_hex = 'xxx'
payload = Payload(alert="Hello World!", sound="default")
apns.gateway_server.send_notification(token_hex, payload)
I ran script, it didn't return errors, but i haven't received notifications at my Ipad. I tried php script from this tutorial, script returns "Message successfully delivered", but i'm still not receive any message. Php code here.
Any ideas how to debug or what is wrong ?

Swift not Parsing JSON from PHP Server

I am writing a simple application for iOS in swift that makes an ajax call to a server of mine. Here is the relevant swift code:
class Request : NSObject {
func send(url: String, f: (NSData)-> ()) {
var request = NSURLRequest(URL: NSURL(string: url)!)
var response: NSURLResponse?
var error: NSErrorPointer = nil
var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: error)
//var reply = NSString(data: data!, encoding: NSUTF8StringEncoding)
f(data!)
}
}
class myObj: NSObject {
let baseURL: String
var message: String
init() {
self.baseURL = XXXXXXXXXXXXX
self.message = "No Message"
}
func check() -> Bool {
let locationURL = self.baseURL
self.message = locationURL
var request = Request()
request.send(locationURL, f: {(result: NSData)-> () in
let jsonData: NSData = result
var error: NSError?
let jsonDict = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &error) as! NSDictionary
self.message = jsonDict["title"] as! String
})
return true
}
}
Here is the server side code that I return dummy JSON with:
<?php
header('Content-Type: application/json; charset=utf-8');
if(isset($_GET['location'])) {
echo json_encode(Array( 'success' => true, 'message' => 'I came from the server.', 'title' => 'tittttt'));
} else if(isset($_POST['message'])) {
echo json_encode(Array( 'success' => true, 'message' => 'message received', 'title' => 'ttt'));
} else {
echo json_encode(Array( 'success' => false, 'message' => 'invalid params', 'title' => 'title from server'));
}
?>
When I switch out my URL for a dummy json url (I am using http://jsonplaceholder.typicode.com/posts/1?a=b) it works; when I use my own URL it fails with the following error:
fatal error: unexpectedly found nil while unwrapping an Optional value
What am I doing wrong here?
EDIT: Here is the actual JSON response from the server:
{
success: true,
message: "I came from the server.",
title: "tit-le" }
Have u tried adding
header("Access-Control-Allow-Origin: *");
To your PHP

Categories