【问题标题】:Message notifications and pull content消息通知和拉取内容
【发布时间】:2015-04-17 13:47:37
【问题描述】:

我知道这可能是一个重复的问题,但其他问题没有回答我的问题。

我正在开发一个应用程序,该应用程序从状态栏中提取通知(特别是新 WhatsApp 消息的通知)并读取其内容。我已经成功提取了通知标题和消息内容。

问题是,当收到多条未读消息时,通知会从使用EXTRA_TEXT 切换到EXTRA_SUMMARY_TEXT(然后返回例如"2 new messages"

必须能够以某种方式分隔消息,就像某些现有应用程序那样(例如,Snowball 将所有消息合并并显示在一个位置,即使已收到多条消息但仍未阅读,也会显示消息内容)。

我知道用户可以通过 Intents 发送消息。但是,我似乎无法访问传入的意图,因此假设 WhatsApp 使用显式意图来发送消息。

Intent i = new  Intent("com.test.testapp.NOTIFICATION_LISTENER");

        Bundle extras = sbn.getNotification().extras;

        if(sbn.getPackageName().contains("com.whatsapp"))
        {
            String title = extras.getString(Notification.EXTRA_TITLE);
            String summary = extras.getString(Notification.EXTRA_SUMMARY_TEXT);
            String msg = extras.getString(Notification.EXTRA_TEXT);

            if(msg != null)
            {
                i.putExtra("notification_event", msg);
            }
            else
            {
                i.putExtra("notification_event", summary);
            }

        }
        else
        {
            i.putExtra("notification_event","...");
        }
        sendBroadcast(i);

我的问题:

如何在不将"2 new messages" 作为内容的情况下显示所有收到的消息,或者有更好的方法吗?

我需要访问消息内容、发件人号码和收到消息的时间,以便将其保存到数据库中。

任何帮助将不胜感激。

【问题讨论】:

    标签: android android-intent android-notifications whatsapp


    【解决方案1】:

    WhatsApp 应用程序具有发送通知的结构,如下所示:

            Case                                 Notification
    
    Message comes from A : Hi                   Title : A    Text: Hi
    
    Message comes from A : How are you          Title : A    Text: How are you
    
                                                Title : A    Text: 2 new messages
    
    
    Message comes from B : Hello                Title : B    Text: Hello
    
                                                Title : B    Text: 1 new message
    
                                                Title : A    Text: 2 new messages
    
                         Title : WhatsApp  Text: 3 new messages from 2 conversation
    ---- Here comes the stacking ----
    
    Message comes from C : Good work            Title : C    Text: Good work
    
                                                Title : C    Text: 1 new message
    
                                                Title : B    Text: 1 new message
    
                                                Title : A    Text: 2 new messages
    
                         Title : WhatsApp  Text: 4 new messages from 3 conversation
    
    
     ---- This way when new sender message comes, previoud notifications also comes and we get callback in NotificationListener ----
    

    最后一个通知带有标题作为包名称:WhatsApp 和文本作为:来自 Y 对话的 X 条消息

    获取文本:

    sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TEXT).toString();
    

    获取标题:

    sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TITLE).toString();
    

    要使用这种堆栈结构,我们需要解析这个通知堆栈并在我们的应用程序中仅显示选择性信息

    希望我的回答能帮助解决您的疑问

    这个答案在:enter link description here

    【讨论】: