Push Notification using GCM - PHP - php

I am trying to implement Push Notification using GCM,
Besically i am trying to send Notification to the GCM using my PHP server which will deliver it to my android application.
I have used code at androidhive!
The device is getting registered successfully but i am not receiving any notification at my device nor anything is visible at Google console.
I am not receiving any response from the link used https://android.googleapis.com/gcm/send or https://android.googleapis.com
Is there anything wrong in this approach?
Can anyone suggest me any authentic source for this?

Realizing that some people are facing issues implementing GCM. I have decided to post solution.
The example on AndroidHive is working perfectly. Its just the matter of correct configuration.
Replace project ID in Android app. (You can get project ID from you google's console link i.e google.com/apis/console/#project:xxxxxxxxxxxx:access)
Replace Server API Key on PHP Server(config.php)
Key for server apps (with IP locking).
You can get your IP from WhatIsMyIP.
Key for browser apps (with referers) will not work.

Related

FCM - Send to all users/devices from a PHP script [duplicate]

I have created a small App that's able to receive Push Notifications from the FCM Console.
What i want to do now is to send a Push-Notifications to all Android Phones that got the app Installed using the API. And this is where i got completely lost.
Is there no way to send it to all Phones without collecting all the registration-ids?
Does this only work with the Console but not with the API?
thanx in advance
Sending a message to all the phones like what you do from the Firebase Web Console is only possible from the Web Console.
If you need this feature from the API you can submit a feature request:
https://firebase.google.com/support/contact/bugs-features/
Another possibility is to have all the client registering to a specific topic via FirebaseMessaging.getInstance().subscribeToTopic(topicName)
In this way you can send a message to the whole topic without collecting the registration-ids manually.

Google Cloud Messaging test application

I'm a PHP developer and I have this assignment where I have to send GCM messages to an android application.
I have zero experience on developing android applications so I would like to know if there's a sandbox or a test application where I can test if my server is working properly.
I'm creating the server with these instructions:
GCM with PHP (Google Cloud Messaging)
It seems that I need a server api key (which I know how to create in the google developer console) but how can I create a blank application? I need exact tutorial since I know nothing about it.
I found this answer earlier: https://stackoverflow.com/a/12437549/1278155 but I have no idea what to do with the .zip file provided.
1
You need to register the App on Google Console. For this you need following information about the application to which you wish to send notifications:
SHA Certificate : This is a key that uniquely identifies the Machine on which the app is being created and the app itself. There is a Command Prompt command to get this key directly.
Package name : Each application has a unique Package Name as you have a URL for PHP projects.
The app developer can provide with these details. Then on successful app registration you will get ProjectCode and a Key. These will be needed in further process
2
Next to go a bit deeper and explain why all this information is required by GCM, you see on device side, the app needs to register the user with GCM and in return it gets a GCM-ID at run time. Then This GCM-ID received is passed on to the server side(You). The developer will need ProjectID and Key that was generated in #1.
3
Now when you trigger your code to fire a GCM, you mention the GCM-IDs to send the notification to specific devices or else you can mention the ProjectID that you receive at the time of app registration with Google Console. These parameters are handed over to Google's Side of things.
4
Now if you pass ProjectID, Google can fetch all corresponding GCM-IDs to this project-ID or else you have specifically passed some GCM-IDs. Then the Google will push a message to all devices corresponding to the GCM-IDs.
5
Still not over, The message reached the target device, but not the target Application on the device. So for this purpose we need the package name at the time of registeration o Google console.

iOs push notifications don't work in production mode

I developed an app using push notifications. To send notifications I used PHP code. I followed the following tutorial: The tutorial
The tutorial works correctly with the development mode, but in production mode doesn't work. I repeated the tutorial using the aps_production certificate and reusing the same private key getting from the keychain.
When I execute the php I receive a successfully message but the device doesn't receive the notification. I was trying to solve the problem and I couldn't do it. I know that the device token is different and I'm using the correct token because the app sends it to a server that stores it.

not being able to establish device-to-device messaging using GCM

I am trying to establish device to device messaging using Google Cloud Messaging. I tried using socket programmng, but the connection used to get lost everytime I moved to the next page. Thus I am using GCM. The plan is to send a message from client's phone and it should be displayed on server's phone, using 3g/wifi and not bluetooth.
I am referring the following link for the same:
http://androidexample.com/Device_To_Device_Messaging_Using_Google_Cloud_Messaging_GCM_-_Android_Example/index.php?view=article_discription&aid=122&aaid=142
It has no errors and is running, but nothing is displaying on the screen. Also a few doubts:
In the code in CommonUtilities.java what should be the Server_Url? I am using XAMPP, so should it be XAMPP's URL?
Also, what should be the Sender_ID?
Also in MainActivity.java, what should be the device IMEI? Do we have to enter that manually, or will the device fetch on ots own.
Also in the php files, its written enter "GCM Registeration ID got from device".Is it client's ID? I am running using Google API and registering my ID in the settings of the emulator before proceeding further.
Can anyone please tell me where am I going wrong? Or even if you have a better idea, please do share here. I am looking for mobile to mobile communication only, not using push notifications.
GCM is working as follow:
1/First of all you have to register your application in google and get your Sender_ID
2/the client connect to GCM cloud in order to register it self (using the SENDR_ID)
3/GCM cloud send a registrationID to the client
4/You should make a server so that you save the registrationID of any device that runs your apps
If you want to send a GCM notification from one client to server
1/the client should connect to your server
2/tell the server to make a demande to the GCM cloud containing the registrationID of the server so that the GCM send the notification to the server
you will find a detailed explanation here

C2DM and PHP Server

I'm trying to write the server side of my android app that uses C2DM. I'm going to write it in php. I basically am just doing tests right now. I copied the chrome to phone example for the actual android app and modified it to my liking. Right now my biggest question is the registration.
Let me get everything straightened out.
When registering the device for the first time, the app talks directly to my server. The server grabs the deviceid and responds back with a 200. Right?
So, on the php side of things, I grab the device id like $deviceid = $_POST['deviceId'] Right?
add it to my database. then respond back to the app.. How exactly do I send a particular response back? I assume in the header? I'm unsure how to do that.
Please confirm or deny that I'm on the right track and clear up any confusion.
Thanks for the help.
You don't respond back to the app. You must register your server with google's servers to get a server authentication token. You then use that server token and the device token in a post request to google's C2DM service to have google's servers send a push notification to the phone. Your app and your server never directly communicate when a push notification is sent in C2DM, it is all done through google, yet you need to implement a mechanism for your server to know the device id of the device it wants to send a message to.
This is a pretty detailed guide, though the server code is in Java:
www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html
Make sure you have really gotten the IDs before sending to the server. If you are using an emulator for testing, you could do an echo over at the php script so the message will appear in your logcat.
Google refreshes expired registration IDs so your application should be able to pick up new notifications/ids from google and handle the message by sending to the database.

Categories