Forgive me for not knowing the correct Android termonology. I come from an iOS background so I'm trying to learn how to do two things correctly in the Android world.
Control "Badges" for Android
Send "Push Notifications" via Distribution (production) vs Development.
Fortunately I found the answer to #1 which is to use Status Bar Notifications thanks to this question.
Now for #2. I already have a GCM setup and can send notifications out to all registered devices. However, sometimes I want to be able to send notifications to all user's phones, and sometimes only to "development" devices. With iOS this is distinguished by devices that have been downloaded from the App Store / Ad-Hoc environment (Distribution) and devices that have been physically plugged into a computer which complies the source onto their device (Development). Is there anything similar to this for Android?
There is no difference between development and distribution in Google Cloud Messaging. All the messages are sent from your server to the same GCM endpoint.
If you want to distinguish between development devices and non-development devices, you'll have to manage it on your server's DB (for each registration ID you store in your server, add a flag that says if it's a development device or not).
EDIT :
Actually there's something else you can do, though I'm not sure it's such a good idea. When the app registers to GCM, it supplies a sender ID (which is a Google API project ID). You can use two different project IDs for development release and production release. The registration IDs returned by the registration process are tied to the project ID. Now, when you send a GCM message from your server, you send it using an API key that is tied to a project ID. If you use the API key that is tied to your development project ID, only registration IDs which are tied to that project ID (i.e. they originated from devices that have the development build) will work. Sending messages with "production" registration IDs will result in MismatchSenderId error (which is similar to APNS in the way that sandbox device tokens are invalid in the production environment and vice versa). I'm not sure that's a good idea, because you don't want to rely on errors from Google for your logic. If you want to send messages only to a subset of your clients, you should manage this subset in your DB.
Related
How is that Google, in Firebase Console Notification composer, allows to set notification target to a specific app package name, yet I read there is no way to do it with API? How to send Firebase message to specific application in project?
Is there some hidden capability Google not exposing to devs?
I know about topics, but it takes extra work to set and subscribe to, plus they won't work right away and require some wait time.
The Notification Composer is an online tool hosted by Firebase.
Therefore it already knows the Server Key (required to send messages) associated to each of your projects.
By design (and for obvious security reasons), the Server Key is implicitely linked to a single app, and you can't use it to send a message to different apps.
I want to create a notification system provider that is not based on Google services or similar. In fact, I want to get information about its overall architecture and needed Android sdk functionality.
The most weird point for me is to understand how to send a notification to an Android device.
I mean, how can I identify the Android device on which my application is installed from millions and millions of other Android devices on the Internet?
And how do I send information to him?
Should I use sockets for this or similar stuff?
simple answer: YOU CAN NOT
before everything else i should correct your question, pusher and FCM are not in the same group at all! you can build somthing like pusher or oneSignal or etc but you can not build something like FCM/APNS
you should understand three simple yet important sentences below:
when you want to pull anything from place_1(e.g. api) to place_2(e.g. browser_client) you most have an identifier of the place_1_resource (which commonly is the uniform-resource-locator of api)
when you want to push anything from place_1(e.g. notification_central_server) to place_2(cellphone_client) you most have an identifier of the place_2_resource
you must know the differences between a real server push with server-push-like technologies like long-pulling or ... and you should be aware that what is intended in this concept is a real server push not any kind of pulling with a push jacket!
if you don't have any identifier for a cellphone which you want to send it a notification, your server dont know where to send that notification so we need a resource_identifier_like for cellphones which is actually a device_token_like and you have just one approach to get this device_token_like and that is the FCM/APNS
FCM is like a dns server containing all identifiers of every android device that google supports (almost every android device) and APNS is just the same but for apple devices
note1: even if your app can obtain it's corresponding device device_token_like it can not be used for push notification if its not registered on FCM/APNS
so when you get that device_token_like identifier of your desired clinet_device now you can use different approaches for sending sth to that clinet_device. there are several approaches like SSE, Webpush, HTTP_server_push, Pushlet and etc but none of these approaches supported by mother_companies of these devices, the only approach that is completely supported and standard is the same approach that FCM/APNS official websites suggests
for example an iranian Incorporation named najva uses webpush to send notifications because of USA sanctions but webPush method works good on browsers and android devices but they didn't even apear on an apple devices
finally i should say that i admire your curiosity to less using anything from a benefit_based Inc. like FCM/APNS in your developing but i strongly recommend these articles and books for you cause i think you didn't learn enough:
wikipedia of push technology
story of some guy who tries to make his own push notification service
Push Technology A Complete Guide - 2020 Edition
Data Push Apps with HTML5 SSE
Short, direct answer
You can't (At least till you create your own ROM)
TL;DR, Reason why?
Before you build your own push notification server, you first need to know how it works internally in android.
Whenever you/your server sends a push notification message to the android client, the SDK processes it and shows you the notification. But when your app is not running (or being killed), your app cannot respond to it since it was not running. In such a case, your notification message is sent to a system service which is known as Google play service. For this even to work, you will first need to bind your app with Google play service and that is what FCM does. FCM SDK registers your app to the operating system service on the first initialization. That FCM service is opened to a port which listened to the incoming message from the server and when it receives the message, it publishes a notification on behalf of your app with a PendingIntent containing the data. Then the PendingIntent is delivered to your app when the user clicks it and then finally your app process the data (or the push message)
So basically, for your server to communicate with the client, It first needs to communicate with the FCM service and for that, FCM gives you a token which identifies the application to register with the internal Google play service.
Simplified furthermore, the workflow is as follows:-
Server send push message ---> FCM ---> Google play service,
If your app is running, it is directly handled by the client SDK So, Google play service --> Your app
If not, then it is delivered by the service itself using PendingIntent So, Google play service --> PendingIntent --->| Publish notification
Totally impossible, Workaround?
There is nothing like impossible because an absolute impossibility doesn't exist. Saying impossible generally means near to impossible. (This is similar to math where also we say tends to infinity because no one has achieved it yet).
To make it work, you need to somehow bind your app to the Google play service and you can't because Google hasn't exposed any direct API to do that. The only possible way is using the FCM ;-) (Bad luck again). So the only possible way is to build your own custom ROM with a custom push service that acts as a client for your Push server and a Server for your Push client (which is your app).
Since the above option tends to impossibility, you have to choose a workaround.
The best among the worst workarounds are:-
To make a malicious SDK.
Malicious because it needs to keep the app running in the background with a service that is connected to a WebSocket endpoint of your server. (Harder in new android versions).
Make use of a database where your push notification is saved and your app checks it periodically using AlarmManager.
Hope you have got the point.
Is GCM the only way for push notifications in android? And is it advisable over other methods (if there are any)?
So let's say my app from a device registers on GCM. Is it the app itself which is assosciated with the device that registers itself, or the user and his email id that register? If my user switches devices in future, will the same 'registration token' work for him ? Or in a similar scenario if a user with another g-mail id registers to my app from the same device, do i push notifications to the same device with 2 different registration tokens?
Extending to what i asked in 2, suppose a single user has his g-mail id working on my app on different devices. Does GCM push the notification to all those devices, or only one of them? And if one, then how does it prioritize?
No, there are other push notification services in Android, like Parse. However, it is strongly recommended to use the default push notification service that is present in any Android device that has Google Play Store, because that reduces battery usage. If you are pushing your app somewhere else though, like Amazon Store, keep in mind that Kindle Fire does not comply with this and doesn't have Google Play Services -- at least as far as I know.
The registration token is very volatile and is not bound at all to an email. Actually, sometimes the same device may get a new GCM ID out of the blue. There are many tutorials on GCM's best practices, but Google's is the best way to start. What you should do to prevent problems is store the GCM id of your device in your database, so you can always know which ID is currently associated to your device. GCM may give you new IDs for no reason, so always parse its response properly and store the new ID it may provide, linking it to the device.
GCM doesn't really care which Google account is your app using. That means you can register as many devices as you want with the same account. You have to manage to which devices you push notifications, because the ID is managed by device.
Be careful: there's been some changes with the API lately, so a lot of unnoficial tutorials you find on the internet may point to deprecated methods.
There are other ways of sending notifications, Tokodu and RabbitMQ for example. I wont go over if X is better than Y, since it's not allowed on StackOverflow.
A unique device token is generated for the device, how you use this device token is totally up to you. You can make it unique for the user by, for example, implementing a login system, so that the device token is created and stored in a database (together with the user information). Let's say if two different users uses your application on a shared device, they can both receive notifications on the same device if you allow them to. It depends on your own implementation.
This again depends on how you chose to implement the GCM features on your client and server side. For example, you can choose to store device tokens for each and every device the user has installed the app on, or you can store the latest device token so that notifications are only sent to one of the user's devices.
I hope this helps clear some things up.
Besides good answers above, if you need a push notification solution for a working environment without Internet access, I suggest SignalR of Microsoft, which has already supported Android and Java (you can find out more at GitHub).
Hope this helps little!
If you have one phone and need to use Google Cloud Messaging, what would you use as client nbr 2? Chrome seems awkward, the simulator is terribly slow. Can you use a Python script as gcm client? I didnt quite understand how to use gcm-client, if it could receive push notifications or not. Can it?
Only an Android application can serve as GCM client. So if you only have one phone, you'll have to use the simulator.
Another (awkward) solution you might try is to install two instances of your application on the same phone. That would require to have two builds of your app with different package names (so that your device will treat them as different applications). Each will register to GCM and get its own registration ID. Then each app can send GCM messages to the other.
I have an application setup where both Android and IOS devices are configured with UA libraries. The issue I'm having is in understanding the purpose of some of these steps or if I am duplicating work. I'll explain the process I have setup first.
On application start-up the devices seem to be registering with UA successfully.
The devices then post their apid's and information to my own service (on a different server).
My server stores the device id information and everything required for push.
For every push enable device that comes in, my server performs a subscribe call to UA. "/api/device_tokens/" for IOS and "/api/apids" for Android.
This server is designed so that when it wants to send push notifications, it queries the list of device id's and tokens it has and sends this information to UA's push api service. "/api/push/" etc...
There is a step which is confusing me however.
What is the difference between step 1 and step 4. Am I basically registering the device tokens and information twice? Once from the devices and once from my own server?
In short, what is the difference between the registration that the Devices perform (as per their relevant libraries) and the registration from my server to UA's api "/api/device_tokens"
Also, where does this come into play:
https://docs.urbanairship.com/display/DOCS/Server%3A+Subscription+API
The term "subscriptions" in the UA system refers to recurring content subscriptions, such as with a magazine-type app. Whereas "registration" is a device registation in the system. The apps do register themselves with the UA servers every time the app is started up (more-or-less). There is generally not a need to re-register the devices before every push, however it should not be harmful to do so. If you are maintaining a list of device ids on your servers, you should be using the Feedback API regularly to prune your list. This will return a list of "deactivated" device ids, so you are not storing unaddressable device ids in your system.
You do not need to worry about the Subscriptions API unless your app is selling subscriptions.