1

Ich erhalte Daten mit fcm und ich möchte eine Benachrichtigung erstellen, die ein Bild enthält. Wenn ich diese Daten bekomme, bekomme ich die URL des Bildes, das ich versuche glide zu laden, glide aber ablehnt aus dem Dienst [FirebaseMessagingService]Ich kann keine Benachrichtigung mit Glide erstellen

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

Bitte fügen Sie log, -Code korrekt geschrieben. –

+0

Die einzige Ausgabe, die ich bekomme, ist notif.getId() und notif.getimage(), ich bekomme keine Ausgabe vom Gleitteil. –

+0

Fügen Sie den Code hinzu, von dem aus Sie die createNotifiation-Methode aufrufen. –

Antwort

1

versuchen mit NotificationTarget. Hier ist der Link Glide — Loading Images into Notifications and AppWidgets

Oder können Sie versuchen, wie diese

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

Ich habe den Link verwendet, den Sie zur Verfügung gestellt haben, und es hat funktioniert, aber die Benachrichtigung ist nur eine einfache Benachrichtigung und ich möchte die Benachrichtigung oder eine große Stilbenachrichtigung anzeigen und erweitern. Wie kann ich das tun? –

+0

versuchen, noti in noti panel zu erweitern. Manchmal zeigt es nur kleinen Stil, wenn kein Platz vorhanden ist. (wenn ur code korrekt ist) –

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 hier ist ein Beispielcode für große Bildmitteilung. –

+2

Diese Antwort kann einige Erklärung fehlen. Nur-Code-Antworten sind nicht sehr nützlich. –

+0

@SergioTulentsev Das ist der Grund, warum ich den Code kommentiere. –

Verwandte Themen