【问题标题】:How to show android GCM notification having single notification Id?如何显示具有单个通知 ID 的 android GCM 通知?
【发布时间】:2014-10-03 21:45:25
【问题描述】:

我想显示新闻 GCm 通知。 收到多条消息后,我想显示 3 个未读消息,4 个未读消息。 请帮助我该怎么做 (抱歉英语不好)

【问题讨论】:

    标签: java android google-cloud-messaging


    【解决方案1】:

    只需计算您有多少未读消息,构建通知并 使用 [a NotificationManager.notify](http://developer.android.com/reference/android/app/NotificationManager.html#notify(int, android.app.Notification)) 有两个版本,一个接受通知 ID,另一个接受标签和 ID, 对于这两种方法,如果已经存在具有相同 id 或具有 (id,tag) 的通知,则任何先前的通知都将被新的通知替换。

    看看a this, 要查看可用的样式和模式并使用适合您情况的样式和模式,这里有一个示例,说明如何根据消息数量发出 Big TextInbox Style 通知

    public static void updateOrSendNotification(Context context, String[] messages) {
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
    
    
        int count = messages.length;
        if (messages.length == 0) {
    
            notificationManager.cancel(NOTIFICATION_ID);
            return;
        }
    
    
        //Intent to be launched on notification click
        Intent intent = new Intent(Intent.ACTION_VIEW,
                null,
                context, MainActivity.class);
    
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
    
        int requestID = (int) System.currentTimeMillis();
        PendingIntent contentIntent = PendingIntent.getActivity(context, requestID,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
        String ticker = context.getString(R.string.new_notification_ticker);
    
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context);
    
        String contentTitle = context.getString(R.string.notification_text_style_title, messages.length);
    
        mBuilder.setSmallIcon(R.drawable.ic_stat_notification)
                .setTicker(ticker)                      // the thicker is the message that appears on the status bar when the notification first appears
                .setDefaults(Notification.DEFAULT_ALL)  // use defaults for various notification settings
                .setContentIntent(contentIntent)        // intent used on click
                .setAutoCancel(true)                    // if you want the notification to be dismissed when clicked
                .setOnlyAlertOnce(true); // don't play any sound or flash light if since we're updating
    
    
        NotificationCompat.Style style;
        if (count > 1) {
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
            style = inboxStyle;
    
            mBuilder.setContentTitle(contentTitle);
    
            for (String r : messages) {
                inboxStyle.addLine(r);
            }
        } else {
            NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
            style = bigTextStyle;
    
            bigTextStyle.setBigContentTitle(messages[0].substring(0, 10).concat(" ..."));
            bigTextStyle.bigText(messages[0]);
        }
    
        mBuilder.setStyle(style);
    
    
        notificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
    

    此方法假定您每次调用它时都会收到您的应用收到的所有新消息, 由于 NOTIFICATION_ID 始终相同,任何以前的通知都将被新通知替换, 你可以像这样测试它

    String[] messages = { "hi john how are you ?", "john you never told me if you'r doing ok !", "john lets go out" }; updateOrSendNotification(this, messages);

    【讨论】:

      【解决方案2】:

      根据您的问题,我假设您正在尝试显示堆叠通知(更新当前通知)

      这里是管理和更新通知的详细方法:http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Managing

      多个通知: How to display multiple notifications in android

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多