Firebase send push notification from devices (IOS)

Firebase send push notification from devices (IOS)


Hello Firend , Here i will create how to send push notification from devices to devices from FCM . 

 Step :-1 Setting FCM and Get devices token 

FCM setting you can getting from google website step by step . when you have get devices token in your app . it's means you can able to send push . here i attach a URL,  step by step you can setup setting for FCM token .
https://firebase.google.com/docs/cloud-messaging/ios/client


Step 2:- API URL and other content  


This is URL to send push notification from firebase "https://fcm.googleapis.com/fcm/send
we have to add two request in requestSerializer  in Authorization we have to pass server-key with add "key=" word . 
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
create dictionary to sending data  
in "to" keyword we have to pass devices token 
{
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
  "data" : {
    ...
  },
}



Step 2:- Send push from devices using HTTP connection 

we have create one cliek event and add this code in click event 
-(void)sendPush
{
     NSDictionary *noti = @{@"body": @"This is a Firebase Cloud Messaging", @"title": @"this is title"};
    NSDictionary *params = @{@"to": @"dbpuoXnzY3o:APA91bH3AE_XCbsLpOXycNULcSh1GHzohEFdopmzkY_n8bm78E9_5fHyY3PLCwi5HtTLU_IoOwfdbDc5qCbyzc6tD9Ahg0EviCfbAmhftmMsPqfi0WC...",@"notification":noti,@"data":data};
    NSLog(@"%@",params);

    [NetworkManager postNotification:params getUrl:@"https://fcm.googleapis.com/fcm/send" success:^(id response) {
         NSLog(@"%@",response);
    } failure:^(NSError *error) {

    }];

}

Step 3:- POST request in AFnetworking 


AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:@"key=AI.......2KBG0NoS8dOqgy4" forHTTPHeaderField:@"Authorization"];

    manager.responseSerializer = [AFJSONResponseSerializer
                                  serializerWithReadingOptions:NSJSONReadingAllowFragments];

    manager.requestSerializer = [AFJSONRequestSerializer serializer];






    [manager POST:URL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:appDelegate.window animated:YES];
        success(responseObject);

        ////NSLog(@"success!");
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
       
       NSLog(@"%@",[error localizedDescription]);
       
    }];

Step 4:- We can also check from postmen 

here i attach a image of postmen , same setting we have to set in postmen and getting push notification from postmen . 


 Thankyou . 

Comments