将来自gcm的android通知堆叠起来
我有一个聊天风格的应用程序。当用户收到消息时,我的服务器正在发送gcm通知 安卓方面,我正在收听这些消息,我触发了一个通知。 现在我正在呼叫。使用相同的ID通知,这样状态栏中就不会有多个通知 我想要实现的是gmail之类的东西。它将所有消息分组到一个通知中,并且可以扩展 我环顾四周,看到了收件箱样式的通知,问题是我不明白如何“附加”来自gcm的消息 因为我构建的所有通知都会删除酒吧中已经存在的通知 所以。。。我想知道是否必须将通知存储在数据库中?当收到一条消息时,我会检索以前的消息并完全创建通知 如果我遵循以上步骤,我将不得不在单击或清除通知时进行监听将来自gcm的android通知堆叠起来,android,notifications,stack,google-cloud-messaging,Android,Notifications,Stack,Google Cloud Messaging,我有一个聊天风格的应用程序。当用户收到消息时,我的服务器正在发送gcm通知 安卓方面,我正在收听这些消息,我触发了一个通知。 现在我正在呼叫。使用相同的ID通知,这样状态栏中就不会有多个通知 我想要实现的是gmail之类的东西。它将所有消息分组到一个通知中,并且可以扩展 我环顾四周,看到了收件箱样式的通知,问题是我不明白如何“附加”来自gcm的消息 因为我构建的所有通知都会删除酒吧中已经存在的通知 所以。。。我想知道是否必须将通知存储在数据库中?当收到一条消息时,我会检索以前的消息并完全创建通知
这有意义吗?或者我是在重新发明轮子,并且已经实现了类似的功能 基本上你走对了方向。是的,您需要将信息保存在某个位置,以便在收到其他推送通知时可以对其进行处理,然后您就知道了摘要内容的外观 下面是一个读取已保存通知(作为JSON)的示例,以便您可以执行分组:
private LinkedList<JSONObject> getPreviousNotifications()
{
File file = new File( getFilesDir(), NOTIFICATIONS_FILENAME );
if ( file.exists() )
{
LinkedList<JSONObject> list = new LinkedList<>();
try
{
BufferedReader reader = new BufferedReader( new FileReader( file ) );
String line;
while ( ( line = reader.readLine() ) != null )
{
list.add( new JSONObject( line ) );
}
reader.close();
}
catch ( JSONException e )
{
Log.debug( "json error" );
e.printStackTrace();
}
catch ( IOException e )
{
Log.debug( "failed to open file for reading" );
e.printStackTrace();
}
return list;
}
return null;
}
谢谢你的解释。你为什么喜欢读写这些文件而不是使用本地数据库呢?我想是吧。访问本地文件系统或SharedReferences非常容易,我不必为做额外的建模或SQL之类的事情而操心。
NotificationCompat.Builder summary = new NotificationCompat.Builder( this )
.setSmallIcon( R.drawable.ic_stat_notify )
.setColor( getResources().getColor( R.color.push_group_background ) )
.setContentTitle( yourSummaryTitle )
.setContentText( yourDetailedTextBuilder.toString() )
.setDefaults( Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE )
.setPriority( Notification.PRIORITY_DEFAULT )
.setTicker( yourSummaryText )
.setGroup( NOTIFICATION_GROUP_KEY )
.setGroupSummary( true )
.setStyle( style )
// You'll want to do this in order to delete the local file if he user swipes away your notification
.setDeleteIntent( deleteIntent )
.setAutoCancel( true );
// go directly to NotificationsActivity
summary.setContentIntent( getPendingIntentForNotifications( this, null ) );