2017-07-11 18 views
0

デバイスのグループに通知を送信したいのですが、一部のデバイスはデバイスを受信し、一部のデバイスは受信しません。私のアプリがバックグラウンドのときにFCM通知を受け取ることができません。私は検索しましたが、私の問題を解決できませんでした。アプリがバックグラウンドのときに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; 
     } 
    } 
} 
+0

(https://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in- [ときFirebaseでバックグラウンドでアプリの通知を処理する方法]の可能性のある重複背景のfirebase) –

答えて

0

Firebase通知は、次の2種類があります

は、これは私のサービスクラスです。

火災報知の詳細については、このリンクをクリックしてください。

How to handle notification when app in background in Firebase

+0

マニフェストにサービスを書くのと同じように、これをやったことがあります。「データ」はAPIコール – kirans

+0

のように使われますが、何も動かず、アプリがフォアグラウンドのときに通知を受け取ります – kirans

関連する問題