Android Push Notifications Without using Firebase Cloud Messaging or any other similar service
Asked Answered
L

2

11

I am developing an android chat application for learning purposes that uses a server- client architecture. I wrote the server side myself in Java (because I am trying to learn) and the client side is an android app. I am using TCP sockets for communication.

Everything works great when the two clients have their apps open and are connected, but I would like client A to still be able to send a message to client B when client B does not have the app open.

After doing some research I realized that I need to use the android push up notifications that will allow my server to communicate with the client (the android app) even when the app is not opened or running by triggering an intent on the client side. I did some more research and every tutorial that I found is using a third party service as a middle man to achieve this , such as Firebase Cloud Messaging, which defeats my purpose of trying to learn by doing things myself.

Because I am doing this for learning purposes I do not want to use any of these services such as Firebase Cloud Messaging, I would like to trigger the intent myself from the server that I wrote in java without using the middle-man.

The main goal here is to send messages from my server to the android app when the app is closed, without using any third party software such as Firebase Cloud Messaging.

I am unsure of how to achieve this and I would appreciate it if anyone could point me at the right direction. Thanks!

Linville answered 3/9, 2018 at 7:25 Comment(3)
Go through this library, understand how they have implemented countly-serverPris
Though I would suggest not to spend time in implementing from scratchPris
This is probably a bit too broad for stackoverflow.Lyublin
U
1

In addition to sahand-zehtabchi I want to say, such the service should best use it's own process using android:process in the Manifest so I won't get killed when the main application was terminated and also should implement to be started again after reboot with permission <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> and building a receiver:

<receiver android:name="de.example.BootBroadcastReceiver">  
  <intent-filter>  
    <action android:name="android.intent.action.BOOT_COMPLETED" />  
  </intent-filter>  
</receiver>

But of course such a very sticky background service is not a very nice architecture regarding battery and memory usage of a device. Using any existing Android system service should be preferred if possible.

Underthecounter answered 3/9, 2018 at 8:8 Comment(2)
How will this service receive find out that a message has been sent from a client?Lyublin
Thank you! although this is not the full picture it definitely pointed me in the right direction. So putting @Sahand-zehtabchi's comment and yours together, I should have a receiver that would start an intent . This intent would open a TCP socket connection to my server, receive the new messages that would probably be held in a queue on the server side, then finally would create an android push up notification. What still is not clear to me is what would trigger this service/intent/receiver to execute every set interval if my the app is not even opened?Linville
I
2

i think when app is in background, you can start a service and open a socket then start listening for new messages, and when new messages receive,you can handle showing a notification with android sdk notification.

Irrefrangible answered 3/9, 2018 at 7:52 Comment(0)
U
1

In addition to sahand-zehtabchi I want to say, such the service should best use it's own process using android:process in the Manifest so I won't get killed when the main application was terminated and also should implement to be started again after reboot with permission <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> and building a receiver:

<receiver android:name="de.example.BootBroadcastReceiver">  
  <intent-filter>  
    <action android:name="android.intent.action.BOOT_COMPLETED" />  
  </intent-filter>  
</receiver>

But of course such a very sticky background service is not a very nice architecture regarding battery and memory usage of a device. Using any existing Android system service should be preferred if possible.

Underthecounter answered 3/9, 2018 at 8:8 Comment(2)
How will this service receive find out that a message has been sent from a client?Lyublin
Thank you! although this is not the full picture it definitely pointed me in the right direction. So putting @Sahand-zehtabchi's comment and yours together, I should have a receiver that would start an intent . This intent would open a TCP socket connection to my server, receive the new messages that would probably be held in a queue on the server side, then finally would create an android push up notification. What still is not clear to me is what would trigger this service/intent/receiver to execute every set interval if my the app is not even opened?Linville

© 2022 - 2024 — McMap. All rights reserved.