如何在iOS上对类似whatsapp的通知进行分组 [英] How to group notifications like whatsapp on IOS

查看:94
本文介绍了如何在iOS上对类似whatsapp的通知进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一些通知,您可以使用GCM进行接收,但是每条通知都显示在一个目标中,因此当您收到2或3条通知时,这会很烦人.

如何将所有通知分组到我的应用程序的一个目标中?我认为这就像android,我必须用一些ID标识通知,但是我没有找到有关它的任何信息.

那是应用程序在后台运行时执行的代码:

 //[START ack_message_reception]func应用程序(应用程序:UIApplication,didReceiveRemoteNotification userInfo:[NSObject:AnyObject]){print(收到的通知:\(userInfo)")//仅当该应用启动了GCM服务时,此方法才有效GCMService.sharedInstance().appDidReceiveMessage(userInfo);//处理收到的消息//[START_EXCLUDE]NSNotificationCenter.defaultCenter().postNotificationName(messageKey,object:nil,userInfo:userInfo)//[END_EXCLUDE]} 

GCM消息代码为:

  array('body'=>'有人想和你一起练习!!!',声音" =>默认",振动" =>"1","time_to_live" =>"1"); 

谢谢您的帮助.

解决方案

我知道这个问题是很久以前提出的,但是我发布此答案可能会对寻找相同解决方案的人有所帮助.可以从iOS 12在客户端对通知进行分组.

您需要做的只是设置一个属性,它将为您完成所有工作.下面是使用示例的说明.

UNMutableNotificationContent *内容;//设置所有属性,例如标题,正文等.在这里,我将解释如何对通知进行分组.

 //将属性设置为组通知content.threadIdentifier = @您的组标识符"; 

说明:我们有一个名为threadIdentifier的属性来对通知进行分组,您只需要将此标识符设置为不同的唯一组标识符,iOS便会处理其余的标识符.它将显示所有具有相同标识符的通知(一组).

示例:如果我们以WhatsApp示例为例,它们基于消息发件人对消息通知进行分组,因此我们可以将消息发件人编号/标识符设置为仅此的threadIdentifier.

content.threadIdentifier = @"messageSenderNumber"

此处是使用分组通知的Apple指南

I have in my app some notifications that you recive with GCM but every notification show in one target so when you get 2 or 3 notifications it makes annoying.

How to group all notifications in one target for my app? I think that will be like android, i have to identify the notification with some ID but i did not find any information about it.

Thats the code that execute when the app is in background:

// [START ack_message_reception]
func application( application: UIApplication,
                  didReceiveRemoteNotification userInfo: [NSObject :AnyObject]) {
    print("Notification received: \(userInfo)")
    // This works only if the app started the GCM service
    GCMService.sharedInstance().appDidReceiveMessage(userInfo);
    // Handle the received message

    // [START_EXCLUDE]
    NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
                                                              userInfo: userInfo)
    // [END_EXCLUDE]
}

And the GCM message code is:

array( 'body' => 'Someone wants to practice with you !!!',
                                            "sound" => "default",
                                            "vibrate" => "1",
                                            "time_to_live" => "1"
                                    );

Thank you for your help.

解决方案

I know this question was asked long ago but I am posting this answer it might help someone looking for the same solution. It is possible to group notifications at client side from iOS 12.

What you need to do is just to set one property and it will do all for you. Below is the explanation with example to do that.

UNMutableNotificationContent *content; // Set all the properties like title, body etc. Here I am just going to explain how you can group notifications.

// Set property to group notifications
content.threadIdentifier = @"your group identifier";

Explanation: We have a property named threadIdentifier to group notifications, you just need to set this identifier to different unique group identifiers and iOS will handle the rest. It will show all the notifications having same identifier as one group.

Example: If we consider WhatsApp example they group messages notifications based on message sender, so we can set message sender number/identifier as threadIdentifier that's it.

content.threadIdentifier = @"messageSenderNumber"

Here is reference to Apple's guide for Using Grouped Notifications

这篇关于如何在iOS上对类似whatsapp的通知进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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