1

私はfcmを使用してデータを受信して​​います。イメージを含む通知を作成したいと思います。このデータを取得すると、グライドを使用してロードしようとする画像のURLが取得されますサービスから[FirebaseMessagingService]グライドを使用して通知を作成できません

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

private final String TAG = getClass().getName(); 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    super.onMessageReceived(remoteMessage); 

    try { 

     if (remoteMessage.getData().containsKey("notification")) { 

      Gson gson = new Gson(); 
      Notif notif = gson.fromJson(remoteMessage.getData().get("notification"), Notif.class); 

      politicoDataRepository.getPolitico(notif.getPolitico_id()) 
        .observeOn(AndroidSchedulers.mainThread()) 
        .subscribe(politico -> { 

          createNotifiation(notif, getApplicationContext()); 

        }, throwable -> Log.e("Error", throwable.getMessage() + " " + TAG)); 

     } else { 
      Log.i("datFirebase", remoteMessage.getFrom() + " " + remoteMessage.getData().get("id")); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     realm.close(); 
    } 


} 

private void createNotifiation(Notif notif, Context context) { 

    final RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_item); 

    remoteViews.setImageViewResource(R.id.notif_image, R.mipmap.ic_launcher); 


    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.mipmap.ic_launcher) 
        .setContentTitle(notif.getTitle()) 
        .setContentText(notif.getMessage()) 
        .setContent(remoteViews) 
        .setPriority(NotificationCompat.PRIORITY_MIN); 

    final Notification notification = mBuilder.build(); 

    if (android.os.Build.VERSION.SDK_INT >= 16) { 
     mBuilder.setCustomBigContentView(remoteViews); 
    } 


    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(141, notification); 


    NotificationTarget notificationTarget = new NotificationTarget(this, remoteViews, R.id.notif_image, notification, 141); 

    Glide.with(this) 
      .load(notif.getImage()) 
      .asBitmap() 
      .into(notificationTarget); 

} 

}

+0

コードが正しく記述され、ログを追加してください。 –

+0

私が得る唯一の出力はnotif.getId()とnotif.getimage()です。私はグライドパートから出力を取得しません。 –

+0

createNotifiationメソッドを呼び出す場所からコードを追加してください。 –

答えて

1

NotificationTargetと試みます。ここでは、リンクGlide — Loading Images into Notifications and AppWidgets

であるか、この

remoteViews.setImageViewBitmapのように試すことができます(R.id.noti_article_1_imageview、getBitmapFromURL (selectedList [0] .getImageUrl()));

remoteViews.setImageViewBitmap(R.id.noti_article_2_imageview、getBitmapFromURL (selectedList 1 .getImageUrl()))。

public Bitmap getBitmapFromURL(String strURL) { 
    try { 
     URL url = new URL(strURL); 
     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; 
    } 
    } 
+0

私はあなたが提供したリンクを使用していますが、通知は単純な通知に過ぎず、私は通知を表示したり拡大したりしたいと思っています。どうすればいいですか? –

+0

notiパネルでnotiを拡張しようとします。スペースがなければ小さなスタイルしか表示されないことがあります。 (もしurのコードが正しい場合) –

0
@Override 
     public void onReceive(final Context context, Intent intent) { 
    // this will update the UI with message 

    Log.d("Keyur", " received broadcast"); 
    new CreateNotification(context).execute(); 
} 

/** 
* Notification AsyncTask to create and return the 
* requested notification. 
*/ 
public class CreateNotification extends AsyncTask<Void, Void, Void> { 
    private NotificationManager mNotificationManager; 

    private Context con; 

    public CreateNotification(Context context) { 
     con = context; 
     mNotificationManager = (NotificationManager) context.getSystemService(Context 
       .NOTIFICATION_SERVICE); 

    } 

    /** 
    * Creates the notification object. 
    * 
    * @see #setBigPictureStyleNotification 
    */ 
    @Override 
    protected Void doInBackground(Void... params) { 
     Notification noti = new Notification(); 

     noti = setBigPictureStyleNotification(); 

     noti.defaults |= Notification.DEFAULT_LIGHTS; 
     noti.defaults |= Notification.DEFAULT_VIBRATE; 
     noti.defaults |= Notification.DEFAULT_SOUND; 

     noti.flags |= Notification.FLAG_ONLY_ALERT_ONCE; 


     mNotificationManager.notify(0, noti); 

     return null; 

    } 

    /** 
    * Big Picture Style Notification 
    * 
    * @return Notification 
    * @see CreateNotification 
    */ 
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
    private Notification setBigPictureStyleNotification() { 
     Bitmap remote_picture = null; 

     // Create the style object with BigPictureStyle subclass. 
     NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle(); 
     notiStyle.setBigContentTitle("YourQuote"); 
     notiStyle.setSummaryText("#BeMotivated #BeInspried"); 
     //notiStyle.setSummaryText("Nice big picture.Nice big picture.Nice big picture.Nice big " + 
     //"picture.Nice big picture.Nice big picture.Nice big picture.Nice big picture" + 
     // ".Nice big picture."); 


     try { 
      /*remote_picture = BitmapFactory.decodeStream((InputStream) new URL(UrlUtils 
        .getUrlUtils().getUrl()) 
        .getContent());*/ 
      remote_picture = new CreateAndSaveBitmap().drawMultilineTextToBitmap(con, 
        BitmapFactory.decodeStream((InputStream) new URL(UrlUtils 
          .getUrlUtils().getUrl()) 
          .getContent()), 
        UrlUtils 
          .getUrlUtils 
            ().getQuota()); 
      Log.d("Keyur", "image is downloaded"); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     // Add the big picture to the style. 
     notiStyle.bigPicture(remote_picture); 

     // Creates an explicit intent for an ResultActivity to receive. 
     Intent resultIntent = new Intent(con, MainActivity.class); 

     // This ensures that the back button follows the recommended convention for the back key. 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(con); 

     // Adds the back stack for the Intent (but not the Intent itself). 
     stackBuilder.addParentStack(ResultActivity.class); 

     // Adds the Intent that starts the Activity to the top of the stack. 
     stackBuilder.addNextIntent(resultIntent); 
     PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

     return new NotificationCompat.Builder(con) 
       .setSmallIcon(R.drawable.icon) 
       .setAutoCancel(true) 

       .setLargeIcon(remote_picture) 

       .setContentIntent(resultPendingIntent) 
       //.addAction(R.drawable.icon, "SHARE", resultPendingIntent) 
       .setSubText("#BeMotivated #BeInspried") 
       .setContentTitle("YourQuote") 
       .setStyle(notiStyle).build(); 
    } 
} 
+0

@Jude Fernandesは大きな画像通知のサンプルコードです。 –

+2

この回答には説明がありません。コードのみの回答はそれほど有用ではありません。 –

+0

@SergioTulentsevそれが私がコードにコメントする理由です。 –

関連する問題