如何显示在android的多个通知 [英] How to display multiple notifications in android

查看:206
本文介绍了如何显示在android的多个通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只收到一个通知,如果有一个通知来了,它取代了previous之一,这里是我的code

 私有静态无效generateNotification(上下文的背景下,字符串消息,
        字符串键){
    INT图标= R.drawable.ic_launcher;
    时长= System.currentTimeMillis的();
    NotificationManager notificationManager =(NotificationManager)上下文
            .getSystemService(Context.NOTIFICATION_SERVICE);
    通知通知=新的通知(图标,邮件,时);

    字符串标题= context.getString(R.string.app_name);

    意图notificationIntent =新的意图(背景下,
            FragmentOpenActivity.class);
    notificationIntent.putExtra(键,键);
    //设置的意图,因此它不会开始一个新的活动
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent意图= PendingIntent.getActivity(上下文,0,
            notificationIntent,0);
    notification.setLatestEventInfo(背景下,标题,邮件,意图);
    notification.flags | = Notification.FLAG_AUTO_CANCEL;

    notification.defaults | = Notification.DEFAULT_SOUND;

    // notification.sound = Uri.parse(android.resource://+
    // context.getPackageName()+your_sound_file_name.mp3);
    notification.defaults | = Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0,通知);

}
 

解决方案

我解决了这样的问题,我...

  / **
     *问题的通知,通知服务器发送邮件的用户。
     * /
    私有静态无效generateNotification(上下文的背景下,字符串消息,
            字符串键,字符串MSGID,字符串branchId){
        INT图标= R.drawable.ic_launcher;
        时长= System.currentTimeMillis的();
        NotificationCompat.Builder nBuilder;
        乌里alarmSound = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        nBuilder =新NotificationCompat.Builder(上下文)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(智能共享 - +键)
                .setLights(Color.BLUE,500,500).setContentText(消息)
                .setAutoCancel(真).setTicker(通知来自smartshare)
                .setVibrate(新长[] {100,250,100,250,100,250})
                .setSound(alarmSound);
        字符串consumerid = NULL;
        整数位置= NULL;
        意图resultIntent = NULL;
        如果(consumerid!= NULL){
            如果(MSGID = NULL和放大器;!&安培;!msgId.equalsIgnoreCase()){
                如果(键= NULL和放大器;!&安培; key.equalsIgnoreCase(哟,至关重要!)){
                    ViewYoDataBase db_yo =新ViewYoDataBase(上下文);
                    位置= db_yo.getPosition(MSGID);
                    如果(位置!= NULL){
                        resultIntent =新的意图(背景下,
                                YoDetailActivity.class);
                        resultIntent.putExtra(ID,的Integer.parseInt(MSGID));
                        resultIntent.putExtra(位置,位置);
                        resultIntent.putExtra(notRefresh,notRefresh);
                    } 其他 {
                        resultIntent =新的意图(背景下,
                                FragmentChangeActivity.class);
                        resultIntent.putExtra(键,键);
                    }
                }否则,如果(键= NULL和放大器;!&安培; key.equalsIgnoreCase(信息)){
                    resultIntent =新的意图(背景下,
                            FragmentChangeActivity.class);
                    resultIntent.putExtra(键,键);
                }。
。
。
。
。
。
            } 其他 {
                resultIntent =新的意图(背景下,FragmentChangeActivity.class);
                resultIntent.putExtra(键,键);
            }
        } 其他 {
            resultIntent =新的意图(背景下,MainLoginSignUpActivity.class);
        }
        PendingIntent resultPendingIntent = PendingIntent.getActivity(背景下,
                notify_no,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
        如果(notify_no&10 9){
            notify_no = notify_no + 1;
        } 其他 {
            notify_no = 0;
        }
        nBuilder.setContentIntent(resultPendingIntent);
        NotificationManager nNotifyMgr =(NotificationManager)上下文
                .getSystemService(context.NOTIFICATION_SERVICE);
        nNotifyMgr.notify(notify_no + 2,nBuilder.build());
    }
 

I am receiving only one notification and if there is another notification comes, it replaces the previous one and here is my code

private static void generateNotification(Context context, String message,
        String key) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context,
            FragmentOpenActivity.class);
    notificationIntent.putExtra(key, key);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notification.defaults |= Notification.DEFAULT_SOUND;

    // notification.sound = Uri.parse("android.resource://" +
    // context.getPackageName() + "your_sound_file_name.mp3");
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);

}

解决方案

I solved my problem like this...

/**
     * Issues a notification to inform the user that server has sent a message.
     */
    private static void generateNotification(Context context, String message,
            String keys, String msgId, String branchId) {
        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        NotificationCompat.Builder nBuilder;
        Uri alarmSound = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        nBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Smart Share - " + keys)
                .setLights(Color.BLUE, 500, 500).setContentText(message)
                .setAutoCancel(true).setTicker("Notification from smartshare")
                .setVibrate(new long[] { 100, 250, 100, 250, 100, 250 })
                .setSound(alarmSound);
        String consumerid = null;
        Integer position = null;
        Intent resultIntent = null;
        if (consumerid != null) {
            if (msgId != null && !msgId.equalsIgnoreCase("")) {
                if (key != null && key.equalsIgnoreCase("Yo! Matter")) {
                    ViewYoDataBase db_yo = new ViewYoDataBase(context);
                    position = db_yo.getPosition(msgId);
                    if (position != null) {
                        resultIntent = new Intent(context,
                                YoDetailActivity.class);
                        resultIntent.putExtra("id", Integer.parseInt(msgId));
                        resultIntent.putExtra("position", position);
                        resultIntent.putExtra("notRefresh", "notRefresh");
                    } else {
                        resultIntent = new Intent(context,
                                FragmentChangeActivity.class);
                        resultIntent.putExtra(key, key);
                    }
                } else if (key != null && key.equalsIgnoreCase("Message")) {
                    resultIntent = new Intent(context,
                            FragmentChangeActivity.class);
                    resultIntent.putExtra(key, key);
                }.
.
.
.
.
.
            } else {
                resultIntent = new Intent(context, FragmentChangeActivity.class);
                resultIntent.putExtra(key, key);
            }
        } else {
            resultIntent = new Intent(context, MainLoginSignUpActivity.class);
        }
        PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
                notify_no, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        if (notify_no < 9) {
            notify_no = notify_no + 1;
        } else {
            notify_no = 0;
        }
        nBuilder.setContentIntent(resultPendingIntent);
        NotificationManager nNotifyMgr = (NotificationManager) context
                .getSystemService(context.NOTIFICATION_SERVICE);
        nNotifyMgr.notify(notify_no + 2, nBuilder.build());
    }

这篇关于如何显示在android的多个通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆