我想向设备组发送通知,但有些设备收到,有些设备没有。当我的应用处于后台时,我无法收到 FCM 通知。我搜索但无法解决我的问题。
这是我的服务类:
public class FCMCallbackService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
NotificationsSqlLiteOperations notification_db_class ;
Bitmap bitmap;
int sum=0;
Context context;
int notice_count,meetings_count,issue_count,assets_count ;
SharedPreferences sharedPref;
SharedPreferences.Editor editor;
int count;
String image;
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
Log.e(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Message data payload: " + remoteMessage.getData());
}
try {
sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
editor = sharedPref.edit();
JSONObject a = new JSONObject(remoteMessage.getData());
String notif = a.getString("data");
Log.e("check notif",notif);
JSONObject data = new JSONObject(notif);
String type= data.getString("type");
Log.e("check of type",type);
count = sharedPref.getInt(type, 0);
count++;
Log.e("count is",count+"");
editor.putInt(type,count);
editor.commit();
if( a.has("image") )
image = a.getString("image");
sendNotification(notif);
}catch (Exception e) {
Log.e(TAG,e.getMessage());
}
}
private void sendNotification( String messageBody )
{
try {
Bitmap bitmap1,logo;
JSONObject b = new JSONObject(messageBody);
Intent intent = new Intent(this, SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("Society Snap")
.setContentText(b.getString("message"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
if( b.has("logo") ) {
logo = getBitmapfromUrl(b.getString("logo"));
notificationBuilder.setLargeIcon(logo);
}
if( b.has("image") ) {
bitmap1 = getBitmapfromUrl(b.getString("image"));
notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap1).setSummaryText(b.getString("message")));
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
int color = 0x008000;
notificationBuilder.setColor(color);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher_society);
}else{
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher_society);
}
// FirebaseAnalytics mFirebaseAnalytics;
// mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
// Bundle bundle = new Bundle();
// bundle.putString(FirebaseAnalytics.Param.ITEM_ID, "1");
// bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, messageBody);
// mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);
// Log.e(TAG,messageBody);
// Context context = this;
// sharedPref = PreferenceManageRr.getDefaultSharedPreferences(context);
// SharedPreferences.Editor editor = sharedPref.edit();
// editor.putString("count", messageBody);
// editor.commit();
// @android:drawable/ic_delete
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
} catch (Exception e) {
Log.e(TAG,"send notif"+e.getMessage());
}
// try
// {
// JSONObject jsonObject =new JSONObject()
//
// Intent intent = new Intent(this, SplashScreen.class);
// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
// PendingIntent.FLAG_ONE_SHOT);
//
// Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
// .setContentTitle("SocietySnap")
// .setContentText(messageBody)
// .setAutoCancel(true)
// .setSound(defaultSoundUri)
// .setLargeIcon(image)
// .setStyle(new NotificationCompat.BigPictureStyle()
// .bigPicture(image).setSummaryText("message"))
// .setContentIntent(pendingIntent);
// if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
// int color = 0x008000;
// notificationBuilder.setColor(color);
// notificationBuilder.setSmallIcon(R.mipmap.ic_launcher_society);
// }else{
// notificationBuilder.setSmallIcon(R.mipmap.ic_launcher_society);
// }
//
//// NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//
// NotificationManager notificationManager =
// (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//
// notificationManager.notify(77/* ID of notification */, notificationBuilder.build());
//
// } catch (Exception e) {
//
// Log.e(TAG,e.getMessage());
// }
}
public Bitmap getBitmapfromUrl(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
return bitmap;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
Firebase 通知有两种类型。
请查看此链接,详细了解 Firebase 通知。
如何在 Firebase 中应用在后台运行时处理通知