我正在使用以下代码发送推送通知。当应用程序在后台运行时,它可以与某些设备一起使用,但不能与其他设备一起使用。我想向所有订阅了global
主题的设备发送推送通知。
public void onMessageReceived(RemoteMessage remoteMessage){
if(remoteMessage.getData().size() > 0){
Log.d("Message data payload: " , String.valueOf(remoteMessage.getData()));
try{
String object1 = remoteMessage.getData().get("data");
JSONObject object = new JSONObject(object1);
icon_url = object.getString("image");
title = object.getString("title");
body = object.getString("message");
//target_url = object.getString("target_url");
if(TextUtils.isEmpty(icon_url)){
getNotification(title,body);
}else{
bitmap = getBitmapFromURL(icon_url);
getNotification(icon_url,title,body);
}
}catch(JSONException e){
e.printStackTrace();
}
}else{
sendNotification(remoteMessage.getNotification());
}
super.onMessageReceived(remoteMessage);
}
public void sendNotification(RemoteMessage.Notification remote){
long when = System.currentTimeMillis();
remoteViews = new RemoteViews(getPackageName(),R.layout.custom_notification_big);
//remoteView = new RemoteViews(getPackageName(),R.layout.custom_notification_small);
//Bitmap bitmap = SetImage(remote.getIcon()) ;
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1410, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if(remote.getIcon() !=null && !remote.getIcon().isEmpty()){
URL appImgUrlLink = null;
try {
appImgUrlLink = new URL(remote.getIcon());
remoteViews.setImageViewBitmap(R.id.image_pic, BitmapFactory.decodeStream(appImgUrlLink.openConnection().getInputStream()));
//remoteView.setImageViewBitmap(R.id.image_pic, BitmapFactory.decodeStream(appImgUrlLink.openConnection().getInputStream()));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else{
}
if(remote.getTitle() != null && !remote.getTitle().isEmpty()){
remoteViews.setTextViewText(R.id.title,remote.getTitle());
//remoteView.setTextViewText(R.id.title,remote.getTitle());
}
if(remote.getBody() != null && !remote.getBody().isEmpty()){
remoteViews.setTextViewText(R.id.text,remote.getBody());
//remoteView.setTextViewText(R.id.text,remote.getBody());
}
NotificationCompat.Builder notificationBuilder1 = new NotificationCompat.Builder(getApplicationContext())
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(remoteView)
.setCustomBigContentView(remoteViews)
//.setCustomContentView(remoteViews)
.setContentTitle("Notification") .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setWhen(when);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
notificationBuilder1.setSmallIcon(R.mipmap.ic_launcher);
} else
{
notificationBuilder1.setSmallIcon(R.mipmap.ic_launcher);
}
notificationManager.notify(1, notificationBuilder1.build());
}
public void getNotification(String icon,String title,String body){
remoteViews = new RemoteViews(getPackageName(),R.layout.custom_notification_big);
remoteView = new RemoteViews(getPackageName(),R.layout.custom_notification_small);
//Bitmap bitmap = SetImage(remote.getIcon()) ;
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1410, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if(icon!=null && !icon.isEmpty()){
/* URL appImgUrlLink = null;
try {
URL url = new URL(icon);
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
System.out.println("Bitmap =====>"+image);
} catch(IOException e) {
System.out.println(e);
}*/
//bitmap = getBitmapFromURL(icon);
//appImgUrlLink = new URL(icon);
remoteViews.setImageViewBitmap(R.id.image_pic,bitmap);
remoteView.setImageViewBitmap(R.id.image_pic, bitmap);
}else{
}
if(title != null && !title.isEmpty()){
remoteViews.setTextViewText(R.id.title,title);
remoteView.setTextViewText(R.id.title,title);
}
if(body != null && !body.isEmpty()){
remoteViews.setTextViewText(R.id.text,body);
remoteView.setTextViewText(R.id.text,body);
}
NotificationCompat.Builder notificationBuilder1 = new NotificationCompat.Builder(getApplicationContext())
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(remoteView)
.setCustomBigContentView(remoteViews)
//.setCustomContentView(remoteViews)
.setContentTitle("Notification")
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_MAX);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
notificationBuilder1.setSmallIcon(R.mipmap.ic_launcher);
} else
{
notificationBuilder1.setSmallIcon(R.mipmap.ic_launcher);
}
notificationManager.notify(100, notificationBuilder1.build());
}
public Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public void getNotification(String title,String body){
remoteView = new RemoteViews(getPackageName(),R.layout.custom_notification_small);
//Bitmap bitmap = SetImage(remote.getIcon()) ;
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1410, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if(title != null && !title.isEmpty()){
//remoteViews.setTextViewText(R.id.title,title);
remoteView.setTextViewText(R.id.title,title);
}
if(body != null && !body.isEmpty()){
//remoteViews.setTextViewText(R.id.text,body);
remoteView.setTextViewText(R.id.text,body);
}
NotificationCompat.Builder notificationBuilder1 = new NotificationCompat.Builder(getApplicationContext())
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(remoteView)
.setCustomBigContentView(remoteViews)
//.setCustomContentView(remoteViews)
.setContentTitle("Notification")
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_MAX);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
notificationBuilder1.setSmallIcon(R.mipmap.ic_launcher);
} else
{
notificationBuilder1.setSmallIcon(R.mipmap.ic_launcher);
}
notificationManager.notify(1, notificationBuilder1.build());
}
}
我也通过在Google上搜索来尝试各种示例。这些示例适用于某些设备,但不适用于所有设备。
是因为它需要特定的Android版本吗?还是我需要检查设备的可比性?还是取决于firebase服务选项?