How to send Native Notifications on Android and iOS in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this tutorial is about creating native notifications on mobile systems like Android and iOS creating notifications in these systems can be done by two ways first using system shadow where you just set the time when the notification should be displayed and second using Cloud systems to display notifications remotely I will focus only on the fierce method because I think that for programming games it's enough because all we want to display is just notification to player to remind them about our game at the beginning we need to install additional package so go to window package manager and Search for notifications and unity registry it is also part of Mobile package so it can be displayed in the features here mobile notifications go to package View and click install okay so now we are ready to create some notifications let's begin with creating new object on scene let's call it native notifications controller and let's create the script and call it Android notifications controller now we need to edit the script so let's open it with your favorite IDE let's remove these functions so we need to create some functions let's start with public void when it was authorization this is not how this function need to be called This is just my name you can name it whatever you want and in this function we need to ask user for permissions to display notifications starting off Android 13.0 developers need to ask users for permission to display notifications so let's start with checking if we already have a permission permission is a part of uniqueengine.android package so it will need to be imported here permission permission and here we need to type Android dot permission dot post notification if we don't have this permission because we have a navigation here we need to ask for this permission so permission dot request user permission and type the same name this is asking for our permission and it's enough now we need to register notification Channel and to do that we need to create another function call it whatever you want but I will use register notification Channel here we need to specify the variable type new Android notification Channel and we need to set ID for something what we want I will call it default Channel we need to also specify name importance and description for this channel we can set the importance here we can choose between default high low and non but let's start with default and if you are familiar with Android you will probably know what this do but if you're not it will just add the category of notification to your system settings and using this you can specify few channels for example you can set channel for ads and channel for gameplay related notifications and user can disable for example this ads Channel but leave gameplay Channel with notifications so this is good to separate some different kinds of notifications and now we need to call Android notification center in that register notification Channel and send this channel okay so let's finally set a method to send our notification and now all we need to do is create new Android notification we can assign it to the variable and we can set title text and fire time for this notification and let's say we want to put this as a parent for this function so we need some strings here let's go with string if I go string text and also let's go with hint fire time in seconds we can also set fire time in minutes hours or whatever you want but I will set this to seconds because I want to show you how this will work and we don't want to wait for minutes or hours we'll just fire this in seconds okay so let's set notification and let's set title to title and the same with text text and now we need to set fire time so let's set notification fire time to system date time now and this is the current time and we need to add something to this so we can type up and we can choose between days hours milliseconds minutes months seconds and some others we will go to seconds and I will add this fire timing seconds variable here and at the end we need to call Android notification center sound notification let's send our notification and we need to specify Channel ID and the same and we have earlier so the name of this channel is here default channel so let's just copy it and paste here okay this is all for sending notifications let's back to Unity and we will create another component which we'll call these functions and when the application starts so let's call it notifications controller create this script I will put it here and edit the script we can add reference to this type so let's copy this Android notifications controller and set it as serialized field private and go with Android notifications controller now they call it using star for example remember that we need to ask for permissions first so let's go with Android notification controller and the request authorization after that we need to set channel so we go to register notification Channel and at the end we can find and send notification and you can specify title text and seconds here let's call it hello world text will be and we need to set seconds Also let's set this for three seconds okay and that's all okay one more thing we need to set reference to Android notifications controller here now we can go to file build settings and you can connect your device at the moment here and if you don't have your device you can use Android Studio emulator which is provided with Android Studio it goes here for virtual device manager and start your emulator it's important to use Android with IP at least 33 which is Android 13.0 or newer because requesting permission to notification is provided by this version in your version you don't need ask for permission okay so when our phone is connected or our emulator we can build and run this application we need to wait few seconds and this will automatically start this application and after three seconds you will get a notification you see that dot here and this is our notification hello award from native notifications app so this is how it works on Android and now we can get to the iOS version because as you may notice we created Android notification controller with functions that use Unity NJ dot Android and you need notification that Android if you want to use it on iOS this will not work this will not even build properly but we'll talk about this later now let's create new component in our 90 notification controller and let's call it iOS notifications controller and let's edit the script so we need to start with requesting authorization to display permissions and to do that we need to create function that returns enumerator and we can name it whatever we want but I will call it request authorization now inside this function we can type using variable and let's say we will name this rig like request and create new authorization request and now we need to put our resolution options here if you want to display notification pop up notification notification that is displayed on the notification center and we need to put here authorization option in that alert and if you also want to display notification dot on the application you need to specify also authorization options that batch you will need to use this character to combine these two options and at the end you can type true or false this is for remote notifications we can set this through but we will not use remote notifications at all so that doesn't matter okay and we need to set loop here while not request is finished we need to hear the return here somewhere to your turtle nil okay so this is for requesting authorization now we want to send this notification and we don't have to create notification channels here we just want to create send notification function and we will also want to put Title Here and also text here this will name body for example we can also use subtitle here so we can go with it and we need to specify time so let's go with seconds also fire time in seconds you can also set this for hours minutes or whatever you want and now let's begin with creating Time trailer we need a variable called for example time trigger and create new object of iOS notification time interval trigger and we need to specify to your time interval which is new time span and here we can set today's hours minutes seconds milliseconds or ticks let's go with seconds so if we want to specify only seconds we need to type zero four hours zero four minutes and for second we have fire time in seconds repeat and set this default because we don't want to repeat this notification this is up to you now let's create notification variable of type iOS notification there are some other options event fire we need to set ID for this or we can abandon this and this will be created automatically but let's say this will be hello world notification if you want to create more notifications you can leave this empty to generate this automatically or you can push this identifier by param of some notification functions the title set to title the same with body and the same with subtitle and now show in foreground option you can set this to true to display this notification and not only in the background but also on foreground and you can specify foreground presentation option to alert and sound this will emit also some on this notification will will be displayed so now we need to set category identifier and also if you have more categories you can specify different identifiers to category you can also put this in parents of some notification functions but let's say we will just name it default category thread identifier big one is okay and let's set this to default and Trigger we need to set to time trigger recreated before we need to call iOS notification center and Shadow notification to notification okay that's all so let's back to Native notification controller because we need to set reference here also private iOS notification controller and name it the same way and here we have a little problem because we have Android notification controller here and when we create the mobile app we can also create it to Android and iOS and if we want to combine these two systems we need to specify which notification controller should be called let's start with just as a notification controller and request authorization this is coding so we need to use it with star 13 and then we need to set send notification let's call it that'll work and we can specify body we can also type subtitle and we need to set time and let's set this to three seconds also okay and now we need to use some preprocessor directives which will be used to determine on which part of the start function should be called we need to type hash if and you need the Android here and we need to end this in this place with alif unity iOS and we can set Endive here now we see that if Unity is set to Android Target this part of code we will be grayed out but if we switch platform to iOS this will change this will be grayed out and this will be used and one more thing is that we use notifications iOS and notifications Android here and if we want to compile it now you will see that this will fail okay so we have our consoles here and as we can see the type or namespace name iOS doesn't exist in the namespace unity notifications this is because we building for Android not for iOS so to fix this problem we can go to iOS notification and we need to set entire content of iOS notifications controller class with if you need the iOS preprocessor we need to end it here and now this functions doesn't exist we need to do the same with unity of notification iOS using here okay so this will fix the problem this function exists on in unity iOS but I assume that you know how to preprocess or work if not just check some tutorials on the internet because I don't want to talk about this in this video okay so we need to set also the processor here for Unique Android and we will and this is fully offended and the same thing for using here and now we won't have problems anymore so let's back to Unity we need to set reference here also and now we can save the scene let's go with build settings and now if you will build for Android you won't get any errors as you can see build is completely for result succeed so let's change this platform to iOS and now we can choose build and run and we need to wait few seconds to complete okay so one application is run on our iOS device we will get a request to sending notifications and we need to allow this and we can close the application and as you can see we have Hello World notification with subtitle and body displayed here I don't get a pop-up because I have don't disturb mode enabled but as you can see this work if we click on this notification this will move us to this application okay and that's all when it comes to sending simple notifications there are also some other triggers and callbacks and icons you can set all informations is on the page of unity mobile notification package you can choose section you are interested the most Android iOS or some other settings I will not tell about all this stuff in this video because I want to keep it simple and not as long okay so this is all for today I hope this tutorial will help you a little I created it because I'm creating a space shooter mobile game you can check this game on the devlog series on my channel because I have to implement notification in my game I decided to create tutorial also and here it is okay so if you like this video hit the thumbs up subscribe to not miss more games related content and see you soon bye
Info
Channel: blazeDev
Views: 6,628
Rating: undefined out of 5
Keywords: Unity, Android, iOS, Notifications, App Development, Mobile Development
Id: KkpWtsljLCA
Channel Id: undefined
Length: 15min 35sec (935 seconds)
Published: Mon May 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.