繁体   English   中英

Android 推送通知:多个通知显示相同的数据

[英]Android push notification : Multiple notifications displays same data

我有一个应用程序,它显示从后端生成的通知,每个通知都包含不同的数据,单击时应在通知页面上显示适当的内容。

例如:假设我收到 3 个通知,其中一个通知的数据为 RED,ID 为 1,然后我收到另一个通知,其中 BLUE 的 ID 为 2,另一个通知的数据为 GREEN,ID 为 3,所以每当我点击任何通知时,都会说 RED、BLUE 或 GREEN总是打开 RED 的通知页面

@Override    
public void onReceive(Context context, Bundle bundle) {
        ctx = context;
        model = new Model();
        String subtitle = bundle.getString("subtitle");
        String title = bundle.getString("title");
        String body = bundle.getString("body");
        String eventJsonString = bundle.getString("extraData");
        JSONObject extraObj = null;
        try {
            extraObj = new JSONObject(eventJsonString);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String Id = null;
        try {
             deviceId = extraObj.getString("Id");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if( Id == null){
            Log.d("Notification","ready notification "+bundle.get("Id"));
            Intent result = new Intent();
            result.setAction(ServiceConstants.NOTIFICATION_RECEIVED);
            LocalBroadcastManager.getInstance(context).sendBroadcast(result);
        }
        else{
    isNotification = true;
            try {
                    model.setId(Integer.parseInt(extraObj.getString("Id")));
            } catch (Exception e) {
                e.printStackTrace();
            }

        //Type is not available with notification bundle
        try {
                model.setType(extraObj.getString("Type"));
        } catch (Exception e) {
            e.printStackTrace();
            model.setType("NA");
        }

    }
    sendNotification(title, subtitle, body, context);

}

private void sendNotification(String title, String subtitle, String body, Context context) {

    Intent intent = null;
    if(isNotification){
        intent = new Intent(ctx, NotificationActivity.class);
        intent.putExtra("model", model);
    }
    else{
        intent = new Intent(ctx, MyActivity.class);
    }
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    mNotificationManager = (NotificationManager)
            ctx.getSystemService(NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);


    String id = "com.example.testnotification";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel mChannel = new NotificationChannel(id, title, NotificationManager.IMPORTANCE_DEFAULT);
        mChannel.enableLights(true);
        mChannel.enableVibration(true);
        mChannel.setLightColor(Color.GREEN);
        mNotificationManager.createNotificationChannel(mChannel);
    }

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(ctx)
                    .setSmallIcon(R.drawable.launcher_icon)
                    .setContentTitle(title)
                    .setChannelId(id)
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(subtitle +"\n" + body))
                    .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                    .setContentText(body);

    mBuilder.setContentIntent(contentIntent);
    mBuilder.setAutoCancel(true);
    int random = (int) (Math.random() * 50 + 1);
    mNotificationManager.notify(random, mBuilder.build());
}

在此处传递唯一 ID

 PendingIntent contentIntent = PendingIntent.getActivity(ctx,id,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM