Reputation: 568
There is problem when sending the notification using firebase the notification in which the title and text is shown but when sending other data the String imgurl = remoteMessage.getData().get("image");
is on null reference how to solve this problem.
Here is my code
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
String imgurl = remoteMessage.getData().get("image");
bitmap = getbitmap(imgurl);
getnotifiacation(bitmap, title, body, imgurl);
}
@Override
public void onNewToken(String token) {
}
private void getnotifiacation(Bitmap bitmap, String title, String body, String imgurl) {
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);
notifiaction = new NotificationCompat.Builder(this, id)
.setAutoCancel(true)
.setContentTitle(title)
.setContentText(body)
.setLargeIcon(getbitmap(imgurl))
.setSmallIcon(R.drawable.ic_launcher_background)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap))
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(id, "notifiaction",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notifiaction.build());
}
public static Bitmap getbitmap(String imgurl) {
try {
URL url = new URL(imgurl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Thank you
Upvotes: 1
Views: 280
Reputation: 568
I got the error.The error was in payload code here is the right code below
1 st
connect your app to firebase then after you have to implements the library for the firebase messaging service.
In Manifest
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
Create class FirebaseMessagingservice
public class FirebaseMessagingservice extends FirebaseMessagingService {
NotificationCompat.Builder notifiaction;
Bitmap bitmap;
String id="Default";
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage)
{
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
String imgurl = remoteMessage.getData().get("image");
//getting custom data from php
bitmap=getbitmap(imgurl);
getnotifiacation(bitmap,title,body,imgurl);
}
@Override
public void onNewToken(String token) {
}
private void getnotifiacation(Bitmap bitmap, String title, String body, String imgurl)
{
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);
notifiaction=new NotificationCompat.Builder(this,id)
.setAutoCancel(true)
.setContentTitle(title)
.setContentText(body)
.setLargeIcon(getbitmap(imgurl))
.setSmallIcon(R.drawable.ic_launcher_background)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap))
.setContentIntent(pendingIntent);
NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(id,"notifiaction",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0,notifiaction.build());
}
public static Bitmap getbitmap(String imgurl)
{
try
{
URL url=new URL(imgurl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream=connection.getInputStream();
Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
return bitmap;
}catch (IOException e)
{
e.printStackTrace();
return null;
}
}// code for converting imgurl into bitmap
and here is the PHP code
When app is background the onMessageReceived is not work for this above payload code will work for you Thank you :)
Upvotes: 2