1
00:00:00,510 --> 00:00:07,410
Welcome back and this video, we are going to start creating the notification functionality in our application

2
00:00:07,620 --> 00:00:08,610
and therefore.

3
00:00:09,560 --> 00:00:15,470
I can recommend to check out the cloud messaging slash Android slash client to understand better how

4
00:00:15,740 --> 00:00:21,410
to set up a firebase cloud messaging client app on Android, even though we're not using a messaging

5
00:00:21,410 --> 00:00:21,620
app.

6
00:00:21,620 --> 00:00:25,940
But it's still useful when it comes to using notifications.

7
00:00:28,200 --> 00:00:36,480
So whenever you want to use that, the first thing you need to do is to add to gradle settings or entry

8
00:00:36,480 --> 00:00:36,830
here.

9
00:00:37,230 --> 00:00:38,730
So inside Bill Gradle.

10
00:00:39,790 --> 00:00:47,050
For the upgrade of module app here, we need to add a new implementation here, which is going to be

11
00:00:47,770 --> 00:00:48,750
the following.

12
00:00:48,760 --> 00:00:57,040
So implementation combat Google or Firebase Kolan Firebase Monist messaging Kolan 20 21. that one is

13
00:00:57,040 --> 00:01:01,180
the current version and my case and we need to sync at once.

14
00:01:01,180 --> 00:01:01,810
We added that.

15
00:01:03,410 --> 00:01:09,050
Even though, of course, the version might change, so here, if you check out this documentation here,

16
00:01:09,050 --> 00:01:11,060
you will always find the latest version.

17
00:01:12,430 --> 00:01:19,570
And then you will need to edit your manifest, so you'll need to add that to your manifest, even though

18
00:01:19,570 --> 00:01:22,960
in our case we will need to make some adjustments.

19
00:01:24,650 --> 00:01:28,220
So let's go over to the manifest file.

20
00:01:29,190 --> 00:01:33,150
And let's at the service here inside of the application.

21
00:01:35,160 --> 00:01:43,050
And for some reason, I didn't copy the starting service part, so let me just give you the final version

22
00:01:43,050 --> 00:01:43,690
directly.

23
00:01:43,710 --> 00:01:46,260
So here this is how it's gonna look like.

24
00:01:46,710 --> 00:01:50,790
I'm going to call this FSM, my firebase messaging service.

25
00:01:51,710 --> 00:01:57,620
Android exported this set falls and then this is the action that we're using, so Android called a name

26
00:01:57,640 --> 00:02:02,030
called Google Dot Firebase Dot messaging on this core event.

27
00:02:02,600 --> 00:02:03,890
OK, so now.

28
00:02:05,510 --> 00:02:11,660
We will also need to add some metal techs to our android manifest here and what this area will disappear

29
00:02:11,660 --> 00:02:13,910
as soon as we create this class.

30
00:02:13,910 --> 00:02:18,590
Here is my firebase messaging service Cross, and we'll do that in a second.

31
00:02:18,590 --> 00:02:22,280
But first, let's add some metal techs and metal techs.

32
00:02:22,280 --> 00:02:24,430
We can add them at the very top.

33
00:02:24,890 --> 00:02:25,700
So here.

34
00:02:27,920 --> 00:02:28,880
Metuh.

35
00:02:29,990 --> 00:02:32,690
Data should be, for example.

36
00:02:34,620 --> 00:02:36,010
The one that you can find here.

37
00:02:36,570 --> 00:02:39,120
So basically we can also just copy that.

38
00:02:41,150 --> 00:02:43,550
And posted to our manifest here.

39
00:02:45,090 --> 00:02:46,680
And then this one is optional.

40
00:02:47,190 --> 00:02:54,110
Let's put that one in there as well so you can just click the copy button and then let's add this metadata

41
00:02:54,360 --> 00:02:55,320
and here as well.

42
00:02:56,130 --> 00:02:59,990
OK, so then you will have these three metadata text with their information in there.

43
00:03:00,840 --> 00:03:04,930
And at that point, we can create a class that is still missing and why we get this error.

44
00:03:04,950 --> 00:03:10,320
So here, let's create this class inside of the folder, which we will also create, because you can

45
00:03:10,320 --> 00:03:15,480
see already it says here that it should be the ACM folder.

46
00:03:15,840 --> 00:03:21,480
OK, so let's create a new folder or a new directory in our project here.

47
00:03:23,060 --> 00:03:27,700
Right, click new package Afkham.

48
00:03:29,310 --> 00:03:29,880
And.

49
00:03:32,440 --> 00:03:39,280
Inside of that, we want to create a new content class, which I'm going to call home Firebase Messaging

50
00:03:39,280 --> 00:03:39,760
Service.

51
00:03:41,140 --> 00:03:45,870
All right, now let's look at dendrite manifest, the error seems to disappear.

52
00:03:47,470 --> 00:03:51,700
Even though we need to make sure that this class is inheriting from.

53
00:03:52,690 --> 00:03:55,520
The firebase messaging service.

54
00:03:55,570 --> 00:03:58,840
So basically everything here except to my.

55
00:04:01,250 --> 00:04:02,620
And that's important as well.

56
00:04:05,430 --> 00:04:13,170
Now we need to add brackets here towards the end and implement our part based messaging service class.

57
00:04:13,980 --> 00:04:18,149
Now there are, of course, multiple methods that we can use here and override.

58
00:04:18,180 --> 00:04:21,839
So let's start with the on message received.

59
00:04:22,140 --> 00:04:24,600
OK, so on message received.

60
00:04:26,220 --> 00:04:30,430
Here, I'm going to rename this one to a remote message because I don't like the name.

61
00:04:31,260 --> 00:04:32,010
So here.

62
00:04:33,420 --> 00:04:37,150
So it's a little more consistent, just p0, it's not very useful.

63
00:04:37,440 --> 00:04:41,810
So when we get the message, this function will be called.

64
00:04:42,090 --> 00:04:43,950
So there are two types of messages.

65
00:04:44,580 --> 00:04:49,660
There are data messages and notification messages, and the data messages are handled here in the on

66
00:04:49,680 --> 00:04:55,980
message received, whether the app is in the foreground or background data messages are the type traditionally

67
00:04:55,980 --> 00:04:57,240
used with GCM.

68
00:04:59,090 --> 00:05:04,790
Whereby GCM stands for Google Cloud messaging notification messages, however, are only received here

69
00:05:04,790 --> 00:05:09,770
and on message received when the app is in the foreground, when the app is in the background automatically

70
00:05:09,770 --> 00:05:10,310
generated.

71
00:05:10,310 --> 00:05:14,500
Notifications are displayed when the user taps on the notification.

72
00:05:14,510 --> 00:05:21,110
Then he's used returned to the application and messages containing both notification and data payloads

73
00:05:21,290 --> 00:05:23,780
are treated as notification messages.

74
00:05:24,760 --> 00:05:30,040
All right, the firebase console always sends notification messages if you want to know more than check

75
00:05:30,040 --> 00:05:32,590
out the firebase documentation for that.

76
00:05:32,800 --> 00:05:37,130
So thing is, if you want to handle such a message.

77
00:05:37,150 --> 00:05:39,340
You can add a log entry here.

78
00:05:39,340 --> 00:05:42,220
So log D with a tag.

79
00:05:43,470 --> 00:05:45,930
Messages from Colin.

80
00:05:48,220 --> 00:05:48,760
Dawla.

81
00:05:50,070 --> 00:05:53,430
Remote message thought from.

82
00:05:55,210 --> 00:06:00,550
OK, so if you want to know where the message comes from, it's important log here, it's import tag

83
00:06:00,550 --> 00:06:01,020
as well.

84
00:06:05,680 --> 00:06:14,980
So here is the way where we can now use to or retell remote messages, then we can also check if the

85
00:06:15,130 --> 00:06:16,240
remote message.

86
00:06:17,140 --> 00:06:19,240
That data is not empty.

87
00:06:20,880 --> 00:06:21,330
Then.

88
00:06:23,020 --> 00:06:29,590
We can also have another log entry, so, for example, if it's not empty, we can just say something

89
00:06:29,590 --> 00:06:33,310
like message, data, payload.

90
00:06:35,200 --> 00:06:41,590
And then we split their remote message data, so remote message dot data.

91
00:06:44,750 --> 00:06:51,350
Then we can check if the message contains a notification, so remote message dot notification.

92
00:06:52,610 --> 00:06:53,480
Tartlet.

93
00:06:54,940 --> 00:07:01,540
And here, the notification is another blow and you can also have another long statement, so let's

94
00:07:01,570 --> 00:07:02,260
copy that.

95
00:07:04,300 --> 00:07:06,610
Message notification body.

96
00:07:10,040 --> 00:07:12,290
And here we can get that by using.

97
00:07:13,220 --> 00:07:14,030
EITE.

98
00:07:15,210 --> 00:07:16,740
Taut body.

99
00:07:22,040 --> 00:07:28,520
All right, so it is our value that is given to us, so the remote message notification.

100
00:07:29,570 --> 00:07:31,910
So you can see that here, this great out area.

101
00:07:33,480 --> 00:07:38,070
All righty, so that is the function on message received then.

102
00:07:39,410 --> 00:07:47,570
We can add another and new token override, so here, this one, p0 will be the token that we need to

103
00:07:47,570 --> 00:07:47,970
pass.

104
00:07:48,170 --> 00:07:56,780
So let's replace the stupid name here with token so called if the instance ID token is updated and this

105
00:07:56,780 --> 00:08:00,590
may occur if the security of the previous token had been compromised.

106
00:08:01,010 --> 00:08:05,340
Note that this is called when the instance ID token is initially generated.

107
00:08:05,540 --> 00:08:08,780
So this is where you would retrieve the token.

108
00:08:09,020 --> 00:08:09,430
All right.

109
00:08:09,440 --> 00:08:13,850
So if you get a new token, well, that's the function that would be called.

110
00:08:14,090 --> 00:08:18,440
And then here we can run a little log entry.

111
00:08:18,980 --> 00:08:22,310
So let's say we want to know about the new token.

112
00:08:22,310 --> 00:08:23,990
Then we would just refresh token.

113
00:08:24,680 --> 00:08:29,210
And here is where we would send the registration to the server with the new token.

114
00:08:29,360 --> 00:08:35,690
OK, so if we have or should create a new token, well, that's where we would then also want to send

115
00:08:36,380 --> 00:08:37,350
the following function.

116
00:08:37,380 --> 00:08:40,770
So private fun, send registration.

117
00:08:40,789 --> 00:08:42,530
This is one that I could get myself.

118
00:08:42,559 --> 00:08:44,940
So this is not an override here.

119
00:08:45,320 --> 00:08:46,610
Registration.

120
00:08:48,040 --> 00:08:54,650
To the server and you, we need to pass in the token, which is a string nullable.

121
00:08:55,610 --> 00:09:02,800
OK, so in this case, well, we need to implement the functionality to send the registration to the

122
00:09:02,800 --> 00:09:03,760
server and so forth.

123
00:09:04,000 --> 00:09:10,120
But in the new token that we would call this function OK, and we need to pass on the token.

124
00:09:12,020 --> 00:09:12,560
Like that.

125
00:09:13,510 --> 00:09:19,480
All right, and at this point, I realized that I imported tag, but I would like to use my own tag.

126
00:09:19,520 --> 00:09:23,470
OK, so I don't want to use this content values tag.

127
00:09:24,340 --> 00:09:29,680
I want to have my very own tech, so if you want to use your own tech, what you can do is you can,

128
00:09:29,680 --> 00:09:34,300
of course, create a variable which is called like that, so you can go ahead and create a companion

129
00:09:34,300 --> 00:09:39,670
object, which you call tag, and I'm going to call my tech my firebase message service.

130
00:09:39,970 --> 00:09:44,980
So then if I do that, of course, I want to get rid of this constant value tag so that I don't have

131
00:09:44,980 --> 00:09:46,000
any interference here.

132
00:09:46,460 --> 00:09:51,200
OK, so the tag for the log should be the tag that I have here and my companion object.

133
00:09:52,030 --> 00:09:52,630
OK, and then.

134
00:09:54,380 --> 00:10:00,050
There is one last function that I want to show you in this video, but as you could see, people really

135
00:10:00,050 --> 00:10:02,010
need to dig deeper into it later on.

136
00:10:02,360 --> 00:10:05,840
So I will create a new function here.

137
00:10:05,840 --> 00:10:12,690
Private fun sent notification with the message body, which is of type strength.

138
00:10:12,710 --> 00:10:15,910
So if you want to send a notification, this function should then be called.

139
00:10:16,250 --> 00:10:22,250
So I'm going to do that is with an intent going to say, OK, let's create an intent where I need to

140
00:10:22,250 --> 00:10:26,090
pass in the context, of course, or that's important here real quick.

141
00:10:26,720 --> 00:10:31,580
Then I will pass on the activity, which is intent should be sent.

142
00:10:31,580 --> 00:10:32,660
So main activity.

143
00:10:32,660 --> 00:10:38,750
So the user should be sent to the main activity once he clicks on the notification as the Java.

144
00:10:42,010 --> 00:10:42,550
And then.

145
00:10:43,620 --> 00:10:51,750
I will add a flag to this or add flags, which will be intent, that flag activity clear top.

146
00:10:52,200 --> 00:10:53,670
So activity.

147
00:10:54,980 --> 00:11:03,740
Clear top this one, as you might remember, this is how we made sure that specific activity is said

148
00:11:03,740 --> 00:11:08,220
to a specific position inside of the stack.

149
00:11:08,360 --> 00:11:14,150
So, for example, if we have two activities and we want to put one at the top, then we would give

150
00:11:14,150 --> 00:11:14,570
it this.

151
00:11:16,200 --> 00:11:16,980
And here.

152
00:11:17,840 --> 00:11:18,310
OK.

153
00:11:20,300 --> 00:11:28,220
And then has the notification is something that is sent to the user and you might be in another application

154
00:11:28,640 --> 00:11:35,480
and we cannot just open an intent from the other application to our application, we need to use a pending

155
00:11:35,480 --> 00:11:35,920
intent.

156
00:11:36,170 --> 00:11:42,740
So here what we can do is we can say pending intent, which should be pending.

157
00:11:43,790 --> 00:11:50,870
Intent does not get activity and let's import betting and therefore so here.

158
00:11:54,230 --> 00:12:00,050
Get activity, and he had a single bullet to pass in the context.

159
00:12:02,260 --> 00:12:04,090
Which is going to be this.

160
00:12:05,200 --> 00:12:07,090
Then the request code.

161
00:12:08,630 --> 00:12:16,520
And the intent and finally the flags and here I'm going to use pending intent, not flag.

162
00:12:17,540 --> 00:12:18,170
One shot.

163
00:12:21,290 --> 00:12:25,870
And this flag says that this activity should or this intent should only be usable once.

164
00:12:26,330 --> 00:12:26,790
All right.

165
00:12:27,230 --> 00:12:32,720
And by the way, if you want to know more about planning and tents, you can check out here, there's

166
00:12:32,720 --> 00:12:34,050
a good description.

167
00:12:34,070 --> 00:12:39,890
So basically, the idea is that you should or by giving a pending intent to another application, you're

168
00:12:39,890 --> 00:12:45,440
granting it the right to perform the operation you have specified as if the other application was yourself

169
00:12:45,740 --> 00:12:47,560
with the same permissions and identity.

170
00:12:47,930 --> 00:12:54,490
So you should be careful about how you both your pending application or pending intents and depending

171
00:12:54,490 --> 00:12:59,330
contents itself is simply a reference to a token maintained by the system describing the original data

172
00:12:59,330 --> 00:13:00,200
used to retrieve it.

173
00:13:01,130 --> 00:13:08,570
OK, so we're using this pending intent and then we will create a channel ID for which I'm going to

174
00:13:08,570 --> 00:13:09,760
create a string.

175
00:13:09,770 --> 00:13:11,750
So let me import are here.

176
00:13:12,620 --> 00:13:20,750
And let's create this string, so let's go over to our strengths here, resources, values, strengths,

177
00:13:21,500 --> 00:13:23,720
and let's create this new new string here.

178
00:13:25,370 --> 00:13:27,990
Right, then we can come back to our channel idea.

179
00:13:28,490 --> 00:13:30,960
So now we have this channel idea and we gave it the name, right?

180
00:13:30,980 --> 00:13:37,620
So it's this default notification channel name, whatever we entered here, which is pojman on the court

181
00:13:37,640 --> 00:13:38,630
notification channel.

182
00:13:39,500 --> 00:13:41,570
So I called it and then.

183
00:13:42,490 --> 00:13:47,980
Now that we have the technology, we can add the default sound urai.

184
00:13:51,190 --> 00:13:58,000
Which is the ring tone manager, don't get default your eye.

185
00:13:59,540 --> 00:14:01,220
What should be done notification?

186
00:14:02,290 --> 00:14:10,990
Ring tone manager type notification, so you can see here the type alarm type all the time, ring tones.

187
00:14:10,990 --> 00:14:17,980
So the user has specified what type of notifications he wants to have, for example, for a notification

188
00:14:18,280 --> 00:14:19,390
such as an ASMs.

189
00:14:19,720 --> 00:14:21,350
And that's the one that you want to use.

190
00:14:21,350 --> 00:14:24,820
So we don't want to use a notification that should be used for alarms only.

191
00:14:25,240 --> 00:14:28,090
So the user would be confused why we are using that feature.

192
00:14:29,490 --> 00:14:35,970
And then we can create the actual notification, so here, if we want to create a notification, we

193
00:14:35,970 --> 00:14:39,360
need to use the notification builder and.

194
00:14:40,660 --> 00:14:46,570
Therefore, we can use notification compact dot builder.

195
00:14:48,950 --> 00:14:52,510
And now here for the build up, we need to pass on a lot of information.

196
00:14:53,420 --> 00:14:57,650
Or we can pass in a lot of steps, so to speak, to add to the border.

197
00:14:57,650 --> 00:15:01,040
So first we need to add the context and then the channel ID.

198
00:15:01,930 --> 00:15:04,690
Which is this idea that we have created here.

199
00:15:09,070 --> 00:15:14,840
And then we can set an icon for this notification, so said small icon.

200
00:15:14,860 --> 00:15:18,570
So this is the icon that is displayed in your notification bar.

201
00:15:18,910 --> 00:15:20,950
So you see the little icon.

202
00:15:20,950 --> 00:15:23,320
And here I'm just going to use Aadhar to rubble.

203
00:15:23,630 --> 00:15:24,670
Thought I see.

204
00:15:26,050 --> 00:15:31,450
That I see notification, at least that's how I'm going to call it, so let's create it.

205
00:15:33,360 --> 00:15:34,650
So go to new.

206
00:15:36,130 --> 00:15:37,060
Victor Asset.

207
00:15:38,030 --> 00:15:40,580
And here we can select the.

208
00:15:41,490 --> 00:15:46,380
Android, there's one here, so let's just use the default android.

209
00:15:46,770 --> 00:15:47,940
I'm going to use this one.

210
00:15:47,940 --> 00:15:51,120
As has said, I see notification here.

211
00:15:51,120 --> 00:15:52,760
You can, of course, play around with the color.

212
00:15:52,770 --> 00:15:54,670
So let's use any color.

213
00:15:54,840 --> 00:15:57,570
So it's a little different than the default, the green one.

214
00:15:58,130 --> 00:15:58,640
All right.

215
00:15:58,650 --> 00:16:01,730
So it's going to be this one here in my case.

216
00:16:02,460 --> 00:16:03,900
Then you can set.

217
00:16:04,880 --> 00:16:08,480
The content title, so the title of the.

218
00:16:10,590 --> 00:16:15,570
Notification and just going to say Title four now, of course, you could create a string for this,

219
00:16:15,570 --> 00:16:19,290
but I'm going to keep it simple this time, then the context text.

220
00:16:19,290 --> 00:16:27,090
So the text that you want to display and it's just going to call is one message then set out to cancel

221
00:16:27,690 --> 00:16:28,290
to true.

222
00:16:29,850 --> 00:16:30,300
So.

223
00:16:31,540 --> 00:16:36,550
Setting this flag will make it so the notification is automatically cancelled when the user clicks it

224
00:16:36,550 --> 00:16:37,270
and the panel.

225
00:16:38,270 --> 00:16:40,090
And yeah, that's pretty much it.

226
00:16:42,410 --> 00:16:48,230
Then we can set the sound and that will be the default sound urai that we just prepared.

227
00:16:48,560 --> 00:16:49,670
So this one up there.

228
00:16:50,590 --> 00:16:58,090
And then we can set that content intent, so if the user clicks on it, then it should open up our.

229
00:16:59,310 --> 00:17:06,119
Main activity, so here pending intent is using our intent, which then uses our main activity, so

230
00:17:06,119 --> 00:17:10,050
basically the user, when he clicks on the notification, should be sent to our application.

231
00:17:11,550 --> 00:17:12,690
Now we can go ahead.

232
00:17:14,140 --> 00:17:23,680
And create a notification manager by using that system service with the context.

233
00:17:25,000 --> 00:17:30,280
That notification underscore service and as the.

234
00:17:31,310 --> 00:17:38,660
Notification service saw here as a notification manager, to be precise, so on notification.

235
00:17:40,010 --> 00:17:40,550
Manager.

236
00:17:42,480 --> 00:17:49,950
And then since Android Orio not notification channel is needed as well, so we can check if built.

237
00:17:51,060 --> 00:17:57,870
Thought version thought as Dick and his creator equal build.

238
00:17:59,540 --> 00:18:08,030
Their version codes thought, oh, so whatever the Android O version code is, we need to import build

239
00:18:08,030 --> 00:18:08,390
here.

240
00:18:09,360 --> 00:18:15,570
Let's do that and then we can set the channel to be notification channel.

241
00:18:16,940 --> 00:18:22,430
So we had to channel it, but we didn't have the channels, so now we need to pass in the channel Heidi.

242
00:18:23,620 --> 00:18:27,400
Then the name, so I'm going to say channel.

243
00:18:29,110 --> 00:18:31,140
Pro-gay Manaj.

244
00:18:32,760 --> 00:18:33,270
Title.

245
00:18:34,680 --> 00:18:42,060
And then the notification manager importance, so here, notification manager, importance, default.

246
00:18:43,260 --> 00:18:50,370
So now let's import this notification channel here class so that we can use it and then we can go ahead

247
00:18:50,370 --> 00:18:58,830
and use the notification manager to create the notification channel based on our channel.

248
00:18:59,690 --> 00:19:00,700
Are we prepared?

249
00:19:01,690 --> 00:19:07,850
All right, and at that point, we can use the notification manager to finally notify.

250
00:19:09,100 --> 00:19:11,500
All right, so notify here, we need to pass.

251
00:19:12,070 --> 00:19:13,450
I'm just going to use zero here.

252
00:19:14,530 --> 00:19:19,690
And then our Nottie Frication Belder Dot Pilt.

253
00:19:22,650 --> 00:19:29,040
OK, so this is a very long function, there's a lot of stuff going on and things that we have not touched

254
00:19:29,040 --> 00:19:33,380
on yet, but basically this is how you can prepare a notification.

255
00:19:33,390 --> 00:19:38,130
And when we call this function now, it will send a notification, so to speak.

256
00:19:40,770 --> 00:19:46,680
All right, so this is just a build up and and the next video, we're then going to actually use this.

257
00:19:47,950 --> 00:19:51,040
But first, we will update the token itself.

258
00:19:51,400 --> 00:19:53,290
OK, so see you in the next video.

