{"id":7439,"date":"2022-09-08T18:40:53","date_gmt":"2022-09-08T13:10:53","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/"},"modified":"2022-09-08T18:40:53","modified_gmt":"2022-09-08T13:10:53","slug":"solved-background-processing-in-swift","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/","title":{"rendered":"[Solved] Background Processing in Swift"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-58933783\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"58933783\" data-parentid=\"58864259\" data-score=\"-1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"js-endorsements\" data-for-answer=\"58933783\">\n<\/div>\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Despite getting downvoted, I still think my solution may be useful to someone new to notifications in iOS development. I switched from local notifications to push notifications setting my remote flask application to handle all the background processing and notification sending with firebase:<\/p>\n<ol>\n<li>\n<p>Follow this tutorial to set your app for receiving push notifications: <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.youtube.com\/watch?v=34kvGKdPN2k\">https:\/\/www.youtube.com\/watch?v=34kvGKdPN2k<\/a> <\/p>\n<\/li>\n<li>\n<p>Create a flask application on your remote server and deploy it. If you don&#8217;t have a remote server, I used a 5\u20ac\/month ubuntu server from <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.digitalocean.com\">https:\/\/www.digitalocean.com<\/a> . then you can follow exactly the instructions here to create and deploy your flask application: <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-serve-flask-applications-with-uswgi-and-nginx-on-ubuntu-18-04\">https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-serve-flask-applications-with-uswgi-and-nginx-on-ubuntu-18-04<\/a> (those instructions works on whatever ubuntu server and they have other tutorials for different OS).<\/p>\n<\/li>\n<li>\n<p>Configure flask with your firebase as a server: <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/firebase.google.com\/docs\/admin\/setup\">https:\/\/firebase.google.com\/docs\/admin\/setup<\/a><\/p>\n<\/li>\n<\/ol>\n<blockquote>\n<p>DISCLAIMER: you should&#8217;t proceed if you haven&#8217;t follow successfully the previous steps.<\/p>\n<\/blockquote>\n<ol start=\"4\">\n<li>\n<p>Now if you are new to Flask, it handle HTTP requests pretty neatly and it&#8217;s very intuitive. <\/p>\n<p>4.1. Open your flask application for editing <code>nano myproject.py<\/code> or <code>vim myproject.py<\/code> etc.<\/p>\n<p>4.2 Add <code>import requests<\/code> and <code>from pyfcm import FCMNotification<\/code> in your imports and this right after the imports: <code>push_service = FCMNotification(api_key=SERVER_KEY)<\/code> where <code>SERVER_KEY<\/code> is your server key from firebase in your project settings.<\/p>\n<p>4.3 Add the following code (every bit of this code in ALL CAPS is something you can change):<\/p>\n<\/li>\n<\/ol>\n<pre class=\"lang-py prettyprint-override\"><code>@app.route('\/MY_REQUEST_NAME')\ndef MY_FUNCTION():\n    # MY_ARGUMENT in this case will be the iOS device token\n    MY_ARGUMENT = str(request.args.get('MY_ARGUMENT'))\n    # do calculations\n    result = push_service.notify_single_device(registration_id=MY_ARGUMENT, message_title=MESSAGE, message_body=BODY)\n    return MY_RESULTS\n    # if you're goal is just sending notification you don't really need a return statement\n<\/code><\/pre>\n<p>4.4 Save and exit then in your server activate your virtual environment <code>source myprojectenv\/bin\/activate<\/code> and type <code>pip install requests pyfcm<\/code><\/p>\n<p>Flask application will trigger the function we just wrote when you type in your browser: <code>http:\/\/your_domain.com\/my_request_name?my_argument=my_argument_value<\/code> or you can use your server public IP address instead of the domain if you don&#8217;t have one.<\/p>\n<ol start=\"5\">\n<li>If you don&#8217;t have an SSL certificate (you&#8217;re using <code>http<\/code> instead of <code>https<\/code>), you have to go back to Xcode and add the following in your <code>Info.plist<\/code>:<\/li>\n<\/ol>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Background-Processing-in-Swift.png\"><img decoding=\"async\" src=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Background-Processing-in-Swift.png\" alt=\"App Transport Security Settings\"><\/a><\/p>\n<ol start=\"6\">\n<li>Go to your <code>AppDelegate<\/code> and modify the following function to store the device fcm_token somewhere. I personally used the <code>defaults.standard<\/code>:<\/li>\n<\/ol>\n<pre class=\"lang-swift prettyprint-override\"><code>func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {\n      print(\"Firebase registration token: \\(fcmToken)\")\n\n      \/\/ Save Token:\n      UserDefaults.standard.set(fcmToken, forKey: \"FCM_TOKEN\")\n\n      NotificationCenter.default.post(name: Notification.Name(\"FCMToken\"), object: nil, userInfo: dataDict)\n      \/\/ TODO: If necessary send token to application server.\n      \/\/ Note: This callback is fired at each app startup and whenever a new token is generated.\n    }\n<\/code><\/pre>\n<ol start=\"7\">\n<li>Finally, in Xcode you can call your <code>http<\/code> request like this:<\/li>\n<\/ol>\n<pre class=\"lang-swift prettyprint-override\"><code>let token = UserDefaults.standard.string(forKey: \"FCM_TOKEN\")!\n        let url : URL = URL(string: \"http:\/\/your_domain.com\/my_request_name?my_argument=\\(token)\")!\n        let task = URLSession.shared.dataTask(with: url)\n        task.resume()\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">3<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Background Processing in Swift <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Despite getting downvoted, I still think my solution may be useful to someone new to notifications in iOS development. I switched from local notifications to push notifications setting my remote flask application to handle all the background processing and notification sending with firebase: Follow this tutorial to set your app for receiving push notifications: &#8230; <a title=\"[Solved] Background Processing in Swift\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/\" aria-label=\"More on [Solved] Background Processing in Swift\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[980,1679,1674,522,830],"class_list":["post-7439","post","type-post","status-publish","format-standard","hentry","category-solved","tag-firebase","tag-flask","tag-remote-server","tag-swift","tag-xcode"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Background Processing in Swift - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Background Processing in Swift - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Despite getting downvoted, I still think my solution may be useful to someone new to notifications in iOS development. I switched from local notifications to push notifications setting my remote flask application to handle all the background processing and notification sending with firebase: Follow this tutorial to set your app for receiving push notifications: ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-08T13:10:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Background-Processing-in-Swift.png\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Background Processing in Swift\",\"datePublished\":\"2022-09-08T13:10:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/\"},\"wordCount\":338,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Background-Processing-in-Swift.png\",\"keywords\":[\"firebase\",\"flask\",\"remote-server\",\"swift\",\"xcode\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/\",\"name\":\"[Solved] Background Processing in Swift - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Background-Processing-in-Swift.png\",\"datePublished\":\"2022-09-08T13:10:53+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/#primaryimage\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Background-Processing-in-Swift.png\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Background-Processing-in-Swift.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Background Processing in Swift\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Background Processing in Swift - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Background Processing in Swift - JassWeb","og_description":"[ad_1] Despite getting downvoted, I still think my solution may be useful to someone new to notifications in iOS development. I switched from local notifications to push notifications setting my remote flask application to handle all the background processing and notification sending with firebase: Follow this tutorial to set your app for receiving push notifications: ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/","og_site_name":"JassWeb","article_published_time":"2022-09-08T13:10:53+00:00","og_image":[{"url":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Background-Processing-in-Swift.png","type":"","width":"","height":""}],"author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Background Processing in Swift","datePublished":"2022-09-08T13:10:53+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/"},"wordCount":338,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"image":{"@id":"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/#primaryimage"},"thumbnailUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Background-Processing-in-Swift.png","keywords":["firebase","flask","remote-server","swift","xcode"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/","url":"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/","name":"[Solved] Background Processing in Swift - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/#primaryimage"},"image":{"@id":"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/#primaryimage"},"thumbnailUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Background-Processing-in-Swift.png","datePublished":"2022-09-08T13:10:53+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/#primaryimage","url":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Background-Processing-in-Swift.png","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Background-Processing-in-Swift.png"},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-background-processing-in-swift\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Background Processing in Swift"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/7439","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=7439"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/7439\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=7439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=7439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=7439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}