Push notifications with big Images using cordova push plugin

强颜欢笑 提交于 2019-12-24 15:18:51

问题


I want to get push notification something like flipkart or myntra does. (Push notification will come with an Big image detailing about offers, on clicking of which will take to offers zone). Does anyone know how to get it done. I have code something like this:

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
            .setDefaults(defaults)
            .setStyle(new NotificationCompat.BigTextStyle().bigText("This is a test for push notification with big images."))
            .setLargeIcon(icon)
            .setSmallIcon(context.getApplicationInfo().icon)
            .setWhen(System.currentTimeMillis())
            .setContentTitle(extras.getString("title"))
            .setTicker(extras.getString("title"))
            .setContentIntent(contentIntent)
            .setAutoCancel(true);

回答1:


Sometime back, I created a pull request for this.

The significant code:

public void createBigPicNotification(Context context, Bundle extras)
{
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String appName = getAppName(this);

    .
    .
    .


    String bigPictureUrl= null;
    bigPictureUrl=extras.getString("bigPicture");
    Bitmap bigPictureBMP = null;
    if (bigPictureUrl != null) {
        try {
        URL url = new URL(bigPictureUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
            bigPictureBMP = BitmapFactory.decodeStream(input);
        } catch (IOException e) {
        e.printStackTrace();
        }
    }

    NotificationCompat.BigPictureStyle bigPicStyle = new
        NotificationCompat.BigPictureStyle();
    bigPicStyle.setBigContentTitle(extras.getString("title"));
    bigPicStyle.setSummaryText(extras.getString("message"));
    bigPicStyle.bigPicture(bigPictureBMP);

    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
            .setDefaults(defaults)
            .setSmallIcon(context.getApplicationInfo().icon)
            .setWhen(System.currentTimeMillis())
            .setTicker(extras.getString("title"))
                    .setContentIntent(contentIntent)
                    .setAutoCancel(true)
                    .setStyle(bigPicStyle);

    String message = extras.getString("message");
    if (message != null) {
        mBuilder.setContentText(message);
    } else {
        mBuilder.setContentText("<missing message content>");
    }

    String msgcnt = extras.getString("msgcnt");
    if (msgcnt != null) {
        mBuilder.setNumber(Integer.parseInt(msgcnt));
    }

    int notId = 0;

    try {
        notId = Integer.parseInt(extras.getString("notId"));
    }
    catch(NumberFormatException e) {
        Log.e(TAG, "Number format exception - Error parsing Notification ID: " + e.getMessage());
    }
    catch(Exception e) {
        Log.e(TAG, "Number format exception - Error parsing Notification ID" + e.getMessage());
    }

    mNotificationManager.notify((String) appName, notId, mBuilder.build());
}


来源:https://stackoverflow.com/questions/28718872/push-notifications-with-big-images-using-cordova-push-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!