I am working with FCM notification for our android application. Here i am having problem with FCM notification response. Which is given below code and response.
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'xxxxxxxxxxxxxxxxx' );
$registrationIds = array('id'=>'xxxxxxxxxxxxxxxx');
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
Response:
{message = here is a message. message, title = This is a title. title }
But above response is not proper json format. Kindly please help me that how to send the FCM notification response as a proper JSON format.
Android code is given below:
public class FireMsgService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.e("remoteMessage-", "remoteMessage--->" + remoteMessage.getData());
// Log.e("getnotification-", "getnotification--->" + remoteMessage.getNotification());
Intent intent = new Intent(this, EventFragment.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1410, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
//.setSmallIcon(getNotificationIcon())
.setContentTitle("Event Application")
.setContentText("Quiz notification")
.setAutoCancel(true)
.setSound(sound)
.setContentIntent(pendingIntent)
.setSmallIcon(getNotificationIcon());
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
private int getNotificationIcon() {
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
return useWhiteIcon ? R.drawable.a_option : R.drawable.a_option;
}
}
i am getting response using remoteMessage.getData().
and Firebase ID class is
public class FireIDService extends FirebaseInstanceIdService {
#Override
public void onTokenRefresh() {
super.onTokenRefresh();
String tkn = FirebaseInstanceId.getInstance().getToken();
Log.e("tkn","tkn---->"+tkn);
}
}
Firebase send the Push notification message as a Map object. To get the map as JSON formate you can use the JSONObject class.
So, The parsing will be like this->
JSONObject jsonData = new JSONObject(remoteMessage.getData());
You can also make a POJO class and parse using GSON library.
Related
I can succesfully recive notifications from Firebase console, but when I call the PHP function to do it, doesn't recive anything. As I saw on internet, this is more less how I can send these notifications. And what I don't understand what is different when I switch to Firebase console (because it works from there). I don't think the problem is from my app, because I send a request to their server and then they should send to my device. It is very possible to make a mistake, please don't be too rude. I am still a beginner. Thank you for your wise answers!
PHP function
function push_notification_android($device_id,$message){
//API URL of FCM
$url = 'https://fcm.googleapis.com/fcm/send';
$api_key = 'MY_KEY';
$fields = array (
'to' => $device_id,
'data' => array(
"message" => $message,
"id" => '1',
),
);
//header includes Content type and api key
$headers = array(
'Content-Type:application/json',
'Authorization: key='.$api_key
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
return $result;
}
{"multicast_id":7370520341381062896,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1611404485967655%6b34551f6b34551f"}]}
By documentation you can send two types of messages to clients:
Notification messages - handled by the FCM SDK automatically.
Data messages - handled by the client app.
If you want to send data message implement your FirebaseMessagingService like this (this is only example what to do, you can use it but improve it with your needs and also test all possible solution to understand how that works)
public class MyFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
handleDataMessage(remoteMessage.getData());
}else if (remoteMessage.getNotification() != null) {
//remove this line if you dont need or improve...
sendNotification(100, remoteMessage.getNotification().getBody());
}
}
private void handleDataMessage(Map<String, String> data) {
if(data.containsKey("id") && data.containsKey("message")){
String message = data.get("message");
int id;
try {
id = Integer.parseInt(data.get("id"));
}catch (Exception e){
e.printStackTrace();
id = 1; // default id or something else for wrong id from server
}
sendNotification(id, message);
}
}
private void sendNotification(int id, String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "appChannelId"; //getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.icon) //TODO set your icon
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.iconpro_round)) //TODO set your icon
.setContentTitle("MyApp")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) notificationBuilder.setPriority(NotificationManager.IMPORTANCE_HIGH);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "MyApp", NotificationManager.IMPORTANCE_DEFAULT);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
if (notificationManager != null) {
notificationManager.notify(id, notificationBuilder.build());
}
}
}
#adnandann's answer explains the reason indeed: you're sending a data message, while the Firebase console always sends a notification message (which is automatically displayed by the operation system).
So you have two options:
Display the data message yourself, which #adnandann's answer shows how to do.
Send a notification message from your PHP code, which you can do with:
$fields = array (
'to' => $device_id,
'notification' => array(
"title" => "New message from app",
"body" => $message
),
);
I have an app, whereby I can send push notification to android phone from php-server. In Firebase console it works perfectly well, I am able to include the url in custom data field, such that when the user clicks on the push notification, a webview is opened, which opens up the url (in the custom data field).
how can i pass the custom data also from the server such that when user clicks on push notification, the url in webview is opened.
Firebase Messaging Service
public class MyFirebaseMessagingService extends FirebaseMessagingService{
private final String CHANNEL_ID="notificcation";
#Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.e("NEW_TOKEN",s);
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
#Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String token = instanceIdResult.getToken();
// Do whatever you want with your token now
// i.e. store it on SharedPreferences or DB
// or directly send it to server
SharedPreferences sharedPreferences=getApplicationContext().getSharedPreferences(getString(R.string.FCM_PREF), Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString(getString(R.string.FCM_TOKEN),token);
editor.commit();
}
});
}
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
handleMessage(remoteMessage.getData().get(Config.STR_KEY));
}
private void handleMessage(String message) {
Intent pushNotification=new Intent(Config.STR_PUSH);
pushNotification.putExtra(Config.STR_MESSAGE,message);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
}
}
you need to send the notification for a specific device using its Token. and if you want to send for more than one device, you can use for loop to send for all.
this code in php will send a notification
<?php
define( 'API_ACCESS_KEY', 'Firebase Cloud Messaging api key' );
$myToken = $_GET["token"];
$title = $_POST["title"];
$notification = $_POST["message"];
$msg =
[
'message' => $notification,
'title' => $title,
'custom_url' => $url
//you can add new data and receive it in android
];
$fields =
[
'registration_ids' => $token,
'data' => $msg
];
$headers =
[
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
?>
see this article. you will be able to do it.
https://medium.com/#chahat.jain0/android-push-notifications-using-firebase-cloud-messaging-fcm-php-and-mysql-da571960aeba
if you have any problem, please leave a comment.
I'm currently implement firebase cloud messaging to receive notification from mysql database when a condition in table row data is met. What i currently have right now is just a simple push notification when i enter a php file using browser. Below is all my coding.
push_notification.php
<?php
function send_notification ($tokens, $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = AAAA0********* ',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
$conn = mysqli_connect("localhost", "root", "", "fcm");
$sql = " Select fcm_token From fcm_notification";
$result = mysqli_query($conn, $sql);
$tokens = array();
if(mysqli_num_rows($result) > 0 ){
while ($row = mysqli_fetch_assoc($result)) {
$tokens[] = $row["fcm_token"];
}
}
mysqli_close($conn);
$message = array("message" => " FCM PUSH NOTIFICATION TEST MESSAGE");
$message_status = send_notification($tokens, $message);
echo $message_status;
?>
MyFirebaseInstanceIDService.java
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
#Override
public void onTokenRefresh() {
String token = FirebaseInstanceId.getInstance().getToken();
registerToken(token);
}
private void registerToken(String token) {
OkHttpClient client = new OkHttpClient();
RequestBody body = new FormBody.Builder()
.add("fcm_token", token)
.build();
Request request = new Request.Builder()
.url("http://192.168.1.5/fcm/register.php")
.post(body)
.build();
try {
client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
}
}
MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
showNotification(remoteMessage.getData().get("message"));
}
private void showNotification(String message) {
Intent i = new Intent(this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle("FCM Test")
.setContentText(message)
.setLights(Color.BLUE,1,1)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0,builder.build());
}
}
fcm_token table
token
Right now how do i able to receive a notification from mysql database when a user balance is less than 0.5 and this is my user table
user table
You have set up your system good enough to do the basic thing of sending and receiving the Push Notification.
From what I get is, you are having a system where you want to send a push notification when the user balance is less than 0.5. You can write code in PHP where there is balance manipulation to check user's current balance and send the Push Notification.
OR
You can set up a cron to check balance of each user and send them notification if there balance is less than 0.5
Extremely grateful for any help with this.. all I want to do is use my php code to send notifications to all users subscribed to topic "global". Does anyone know why it might not work? Since I want everyone using the app to get the notifications, I will subscribe everyone (unless there is a better way). Here is my php to try to send the notification to my topic global:
<?php
define( 'API_ACCESS_KEY', 'hidden...hidden' );
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
'vibrate' => 1,
'sound' => 1
);
$fields = array
(
'to' => "/topics/global",
'data' => $msg,
'priority' => 'high'
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec( $ch );
curl_close( $ch );
echo $result;
?>
I am lacking knowledge but from the $result echo it didn't look like any failure message. This is what I got:
{"message_id":7591682951632927615}
In my Firebase console, I cannot even see the topic "global" so I can't test that sending to the topic works on my device. From what I read online, it could take awhile for a subscribed topic to appear in the console. Just to clarify, sending notifications to all devices using user segment set to the App works in the console!
Is there anything that I can do to verify that my app is actually subscribing users to the topic "global"? Maybe this is the problem. Here is the relevant swift code:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FIRApp.configure()
if #available(iOS 10.0, *) {
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
// For iOS 10 data message (sent via FCM)
FIRMessaging.messaging().remoteMessageDelegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
return true
}
func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
print("applicationReceivedRemoteMessage")
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
if let refreshedToken = FIRInstanceID.instanceID().token() {
print("InstanceID token: \(refreshedToken)")
FIRMessaging.messaging().subscribe(toTopic: "/topics/global")
}
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
/*
// Print message ID.
if let messageID = userInfo["gcmMessageIDKey"] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
*/
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
if application.applicationState == UIApplicationState.active {
print("GOT IN HERE")
var pushNotificationMessage = ""
if let aps = userInfo["aps"] as? NSDictionary {
if let alert = aps["alert"] as? NSDictionary {
if let message = alert["message"] as? NSString {
pushNotificationMessage = message as String
}
} else if let alert = aps["alert"] as? NSString {
pushNotificationMessage = alert as String
}
}
let notificationAlert = UIAlertController(title: nil, message: pushNotificationMessage, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
})
defaultAction.setValue(Constants.activePushNotificationOKColor, forKey: "titleTextColor")
notificationAlert.addAction(defaultAction)
self.window?.rootViewController?.present(notificationAlert, animated: true, completion: nil)
}
}
To send a notification, store the parameters in notification, not data:
$fields = array
(
'to' => "/topics/global",
'notification' => $msg, // <= CHANGED
'priority' => 'high'
);
Also look at Table 2a in the documentation for Notification payload support. message is not supported, use body instead.
$msg = array
(
'body' => 'here is a message. message', // <= CHANGED
'title' => 'This is a title. title',
'vibrate' => 1,
'sound' => 1
);
I am using php curl to push notification, Help me adding icon to my notification
i am using below php code for push notification
Using cordova firebase plugin in my app
Working on a hybrid app using phonegap,cordova
Currently Just doing this for android app
function sendFCM($title,$message, $id,$additional_data="") {
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'registration_ids' => $id, //Device Ids
'data' => array (
"additional_data" =>$additional_data
),
'notification' => array(
'title' => $title,
'body' => $message,
'sound'=> 'default'
)
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . "Server Key",
'Content-Type: application/json'
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
curl_close ( $ch );
}
Ty this:
fcm push notification code using android app
We have created to classes:
Notification class
Token service Class
Fcm Notification Service Class
public class NotificationService extends FirebaseMessagingService {
private static final String TAG = "FCM";
boolean notification, sound, vibration;
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, remoteMessage.getNotification().getBody()+"");
Log.d(TAG, remoteMessage.getNotification().getTitle()+"");
Log.d(TAG, remoteMessage.getFrom()+"");
Log.d(TAG, remoteMessage.getData()+"");
addNotification(remoteMessage.getNotification());
}
private void addNotification(RemoteMessage.Notification data) {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon) // add icon
.setContentTitle(data.getTitle()+"")
.setAutoCancel(true)
.setContentText(data.getBody()+"");
Notification notification = new Notification();
if (sound)
notification.defaults |= Notification.DEFAULT_SOUND;
if (vibration)
notification.defaults |= Notification.DEFAULT_VIBRATE;
builder.setDefaults(notification.defaults);
Intent notificationIntent = new Intent(this, Service_confirmedActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
}
Token service class
public class TokenService extends FirebaseInstanceIdService {
private static final String TAG = "FirebaseIDService";
public static String DeviceToc = "";
#Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.e(TAG, "Refreshed token: " + refreshedToken);
DeviceToc = refreshedToken;
Log.e("DeviceToc",""+refreshedToken);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
SharedPreferences.Editor editor = pref.edit();
editor.putString("deviceToc",token); // Storing string
editor.commit();
Log.i("token",""+token);
}
}
it helps you