推送通知的自定义布局

时间:2015-05-14 16:17:52

标签: android parse-platform push-notification

我正在使用Parse.com发送推送通知,并为通知创建了自定义布局。

为此,我将课程ParsePushBroadcastReceiver扩展为:

public class PushBroadcastReceiver extends ParsePushBroadcastReceiver {
    @Override
    protected Notification getNotification(Context context, Intent intent) {
        Notification notification = new NotificationCompat.Builder(context)
                .addExtras(intent.getExtras())
                .setSmallIcon(R.drawable.push_icon)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.push_icon))
                .setVibrate(new long[]{0, 100, 200, 100, 200, 100})
                .setStyle(new NotificationCompat.BigTextStyle())
                .build();

        Bundle extras = intent.getExtras();

        JSONObject json = new JSONObject();
        String title = "";
        String message = "";
        String color = "";

        try {
            json = new JSONObject(extras.getString("com.parse.Data"));
            title = json.getString("alert");
            message = json.getString("message");
            color = json.getString("color");
        } catch (JSONException e) {
            Log.e("PushBroadcastReceiver", "Could not parse notification JSON:\n" + extras.getString("com.parse.Data"));
            e.printStackTrace();
            return null;
        }

        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
        contentView.setTextViewText(R.id.title, title);
        contentView.setTextViewText(R.id.message, message);
        contentView.setInt(R.id.color, "setBackgroundColor", Color.parseColor(color));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            notification.bigContentView = contentView;
        } else {
            notification.contentView = contentView;
        }

        return notification;
    }
}

问题更新:

布局适用于发送的第一个推送通知。每当设备收到另一个通知时,前一个将被刷新",并且布局将是默认布局,而不是自定义布局。

为了始终保持相同的布局,我需要做什么?

0 个答案:

没有答案
相关问题