Tags
Android
Asked 2 years ago
5 Oct 2021
Views 1453
Margarett

Margarett posted

Android - FCM give success but Notification is not received by device in Android


<service
            android:name=".fcm.MyFirebaseMessagingService"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>



package com.example.fcm;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
 import android.util.Log;


import androidx.core.app.NotificationCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.wholesaleveggy.ui.SigninActivity;

/**
 *  Created by archirayan on 29/5/19.
    Author:www.archirayan.com
    Email:info@archirayan.com
 */
public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = MyFirebaseMessagingService.class.getName();
    private Context context;
    private String event_title, event_date;


    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        context = MyFirebaseMessagingService.this.getApplicationContext();
        Log.e(TAG, "From: " + remoteMessage.getFrom());

        if (remoteMessage.getData().size() > 0) {

            event_title = remoteMessage.getData().get("push_title");
            event_date = remoteMessage.getData().get("push_message");
            sendNotification(event_title);
        }

        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body:" + remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getNotification().getBody());
        }


    }

    private void sendNotification(String str) {
        Intent intent = new Intent(context, SigninActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                 .setContentTitle("WV")
                .setContentText(str)
                //.addAction(new NotificationCompat.Action(R.mipmap.ic_launcher_round, "Accept", pendingIntent))
                //.addAction(new NotificationCompat.Action(R.mipmap.ic_launcher_round, "Cancle", pendingIntent))
                .setAutoCancel(true)
                //.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap))
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    }
}



i am not getting notification at android device with given device token
jabber

jabber
answered Oct 9 '21 00:00

I think the problem at serverside code,
you don't mention which server-side scripting language you are using .so I am considering that you are using PHP.

There are two types
1. Notification
Notification will show when app is in foreground.

$title="Test message Title";
$body="Main notification message should be here";
$serverKey='define server key is here';
$notification = array('title' =>$title , 'body' => $body, 'sound' => 'default', 'badge' => '1');
$arrayToSend = array(
'to' => $token, 
'notification' => $notification,'priority'=>'high', 
);
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
 curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
$response = curl_exec($ch);


above PHP code send a notification to the selected device.
2. Data
Data will be shown as a notification when the app is running in the background or killed.

$title="Test message Title";
$body="Main notification message should be here";
$serverKey='define server key is here';
  $arrayToSend = array('to' => $token,'priority'=>'high', 'data' => array(
                        "type" => 104, // post,
                        "type_name" => "post",
                   
                        "title" => $title,
                        'badge' => '1',
                        "body" => $body,
                        'vibrate'   => 1,
                        'sound'     => 1
                    ));
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
 curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
$response = curl_exec($ch);


if you want to send both notification and data is below :

$title="Test message Title";
$body="Main notification message should be here";
$serverKey='define server key is here';
  $notification = array('title' =>$title , 'body' => $body, 'sound' => 'default', 'badge' => '1');
    $arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high', 'data' => array(
                        "type" => 104, // post,
                        "type_name" => "post",
                   
                        "title" => $title,
                        'badge' => '1',
                        "body" => $body,
                        'vibrate'   => 1,
                        'sound'     => 1
                    ));
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
 curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
$response = curl_exec($ch);



Hope it works !
Post Answer