如何在Android中发送这些类型的推送通知?

时间:2016-06-06 12:46:48

标签: android android-notifications

如何使用文字和图像发送这些类型的推送通知,如下图所示?

IMAGE

3 个答案:

答案 0 :(得分:0)

每当我们关心NotificationCompat.Builder对象Flow那个步骤

int icon = R.drawable.reload_logo;

setSmallIcon() methed

中设置图标
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                        mContext);
                Notification notification = mBuilder.setSmallIcon(icon).setTicker("Reload.in").setWhen(0)
                        .setAutoCancel(true)
                        .setContentTitle("Reload.in")
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(intent.getExtras().getString("message")))
                        .setContentIntent(contentIntent)
                        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))

                        .setContentText(intent.getExtras().getString("message"))
                        .setStyle(notiStyle).build();

答案 1 :(得分:0)

使用以下代码:

public class MyGcmListenerService extends GcmListenerService {

    private static final String TAG = "MyGcmListenerService";

    @Override
    public void onMessageReceived(String from, Bundle data) {
        GCMResponse gcmResponse = new GCMResponse(data);
        sendNotification(gcmResponse);
    }

    private void sendNotification(GCMResponse gcmResponse) {
        FetchBitmap fetchBitmap = new FetchBitmap(gcmResponse);
        fetchBitmap.execute();
    }

    class FetchBitmap extends AsyncTask<Void, Void, Bitmap> {

        private GCMResponse gcmResponse;

        public FetchBitmap(GCMResponse gcmResponse) {
            this.gcmResponse = gcmResponse;
        }

        @Override
        protected Bitmap doInBackground(Void... params) {
            Bitmap theBitmap = null;
            if (Looper.myLooper() == null) {
                Looper.prepare();
            }
            try {
                theBitmap = Glide.
                        with(MyGcmListenerService.this).
                        load(gcmResponse.getImageUrl()).
                        asBitmap().
                        into(-1, -1).
                        get();
            } catch (final ExecutionException | InterruptedException e) {
                Log.e(TAG, e.getMessage());
            }
            return theBitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {

            Intent intent = new Intent(MyGcmListenerService.this, Home.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

            int requestID = (int) System.currentTimeMillis();
            PendingIntent pendingIntent = PendingIntent.getActivity(MyGcmListenerService.this, requestID, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MyGcmListenerService.this)
                    .setSmallIcon(R.drawable.logo)
                    .setContentTitle(gcmResponse.getTitle())
                    .setContentText(gcmResponse.getMessage())
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

            if (bitmap != null) {
                // Create the style object with BigPictureStyle subclass.
                NotificationCompat.BigPictureStyle notificationStyle = new
                        NotificationCompat.BigPictureStyle();
                notificationStyle.setBigContentTitle(gcmResponse.getTitle());
                notificationStyle.setSummaryText(gcmResponse.getMessage());

                notificationStyle.bigPicture(bitmap);

                notificationBuilder.setStyle(notificationStyle);
            } else {
                notificationBuilder.setStyle(new NotificationCompat.BigTextStyle()
                        .bigText(gcmResponse.getMessage()));
            }

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            notificationManager.notify(1, notificationBuilder.build());
        }
    }
}

其中GCMResponse.java

public class GCMResponse {

    public static final String GCM_MESSAGE = "message";
    public static final String GCM_TITLE = "title";
    public static final String GCM_IMAGE_URL = "image_url";

    private String message;
    private String title;
    private String imageUrl;

    public GCMResponse(Bundle bundle) {
        message = bundle.getString(GCM_MESSAGE);
        title = bundle.getString(GCM_TITLE);
        imageUrl = bundle.getString(GCM_IMAGE_URL);       
    }

    public String getMessage() {
        return Strings.nullSafeString(message);
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getTitle() {
        return Strings.nullSafeString(title);
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getImageUrl() {
        return Strings.nullSafeString(imageUrl);
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }
}

向build.gradle添加compile 'com.github.bumptech.glide:glide:3.6.1' 它有助于使用URL下载图像。

您只需要在GCM推送中发送3个数据点,此侦听器将获取数据,从URL下载图像并显示推送通知。

答案 2 :(得分:0)

如果你在推送通知json中得到isPicture不为null,那么你应该这样做:

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle(contentTitle)
        .setContentText(contentText)
        .setAutoCancel(true)
        .setSound(defaultSoundUri)
        .setContentIntent(pendingIntent);

if (isPicture.equals("true")) {
    URL url = null;
    Bitmap bmp = null;
    try {
        url = new URL(bigPicture);
        bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    } catch (Exception e) {
        e.printStackTrace();
    }

    NotificationCompat.BigPictureStyle bigPicStyle = new NotificationCompat.BigPictureStyle();
    bigPicStyle.bigPicture(bmp);
    bigPicStyle.setBigContentTitle(contentTitle);
    bigPicStyle.setSummaryText(message);
    notificationBuilder.setStyle(bigPicStyle);
}

NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

此处bigPicture是您想要展示的图片网址。

相关问题