Previous article confirmed that push notification can be sent from the server to Android, so finally check how to run the process on the Android side using that as a trigger. To do.
As summarized in here, when the app is in the foreground and when it is in the background And, the treatment changes slightly. Each is shown below.
If your app is in the foreground, you can receive messages by extending the FirebaseMessagingService
class of the com.google.firebase.messaging.FirebaseMessagingService
package. Two previous articles corresponds to MyFirebaseMessagingService.kt
. In the following, this will be called Iji.
The specifications of the FirebaseMessagingService
class are summarized in here. Several callback methods are provided, and the extension class will override them.
--ʻOnDeletedMessages () : When a message is deleted as a result of the message being left for a long time for some reason, such as the terminal not accessing the server for a long time. --ʻOnDestroy ()
: Unknown because nothing is written. Probably runs when killing the app.
--ʻOnMessageReceived (RemoteMessage message) : When a message is received. --ʻOnMessageSent (String msgId)
: When the message is successfully sent to the connection server of GCM.
--ʻOnNewToken (String token) : When a new token is generated. --ʻOnSendError (String msgId, Exception exception)
: When sending a message to the GCM connection server fails.
When the app is in the background, the notification is delivered to the device's system tray and the data payload is delivered to the additional part of the launcher activity intent. It is written as
. After checking various things, it seems that it should be taken out from ʻintent.getExtras ()`.
The message sent from the server is almost the same as Previous article. However, only the data part is modified as follows.
data={
'date': '20200628',
'message': 'This is a test message'
}
Of the client side, MyFirebaseMessagingService.kt
looks like this.
class MyFirebaseMessagingService: FirebaseMessagingService() {
override fun onNewToken(token: String) {
Log.d(TAG, "Refreshed token: $token")
}
override fun onMessageReceived(p0: RemoteMessage) {
super.onMessageReceived(p0)
val date = p0.data["date"]!!.toInt()
val message = p0.data["message"]!!
val dao = NotificationsDAO(this)
dao.createNotification(MyNotification(-1, date, message)) //Data is stored in MySQL (explanation omitted)
}
}
And MainActivity.kt
is as follows.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onStart() {
super.onStart()
val dao = NotificationsDAO(this)
if (intent.getExtras() != null) {
val date = intent.getExtras().getString("date").toInt()
val message = intent.getExtras().getString("message")
dao.createNotification(MyNotification(-1, date, message)) //Data is stored in SQLite (explanation omitted)
}
val messageList = dao.getAll() //All data has been acquired from SQLite (explanation omitted)
val listView: ListView = findViewById(R.id.listView)
listView.adapter = MessageListAdapter(this, android.R.layout.simple_list_item_1, messageList.take(10))
}
}
When a request was issued from the server in this state, it was confirmed that the notification flew as expected and that the data was stored in SQLite on the Android side as shown in Fig. 1. (The display of ListView
is formatted by the MessageListAdapter
class. The explanation is omitted this time.)
Figure 1 Display of the app after receiving data.
I was able to confirm what I wanted to do, so this survey is over.