【问题标题】:Unable to create notification using glide无法使用滑行创建通知
【发布时间】:2017-04-08 14:58:08
【问题描述】:

我正在使用 fcm 接收数据,我想创建一个包含图像的通知。当我得到这个数据时,我得到了我尝试使用 glide 加载的图像的 url,但 glide 拒绝从服务中触发 [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);

}

}

【问题讨论】:

  • 请添加日志,代码写对了。
  • 我得到的唯一输出是notif.getId()和notif.getimage(),我没有从滑翔部分得到输出。
  • 请在您调用 createNotifiation 方法的地方添加代码。
  • @KeyurThumar 添加了更多代码
  • 请检查remoteMessage是否获取图片url

标签: android firebase firebase-cloud-messaging android-glide firebase-notifications


【解决方案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 (selectedList1.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;
    }
  }

【讨论】:

  • 我已经使用了您提供的链接并且它有效,但通知只是一个简单的通知,我想显示和扩展通知或大样式通知。我该怎么做?
  • 尝试在通知面板中展开通知。有时,如果没有空间,它只会显示小风格。 (如果你的代码是正确的)
【解决方案2】:
   @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();
    }
}

【讨论】:

  • @Jude Fernandes 这里是大图通知的示例代码。
  • 这个答案可能缺少一些解释。纯代码答案不是很有用。
  • @SergioTulentsev 这就是我在代码中注释的原因。
猜你喜欢
  • 2018-06-20
  • 1970-01-01
  • 2022-11-17
  • 2020-07-07
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 2018-07-01
  • 1970-01-01
相关资源
最近更新 更多