在同一堆栈中分组不同的通知

时间:2016-06-21 12:48:10

标签: android notifications

我有一个简单的应用程序,可以在按钮Click上生成通知。我还有一个editText,以便用户输入内容Text。我有两个按钮,生成两个不同的通知。我的要求是,每按一次按钮,应该只生成一个通知,其余应该在通知堆栈中。我已经尝试使用通知setGroup属性,但该方法似乎不起作用。我已在下面附上我的代码以供进一步参考。

主要活动(Java文件): -

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    private static final String GROUP_NOTIFICATIONS="group_notifications";
    NotificationCompat.Builder notification;
    NotificationCompat.Builder notification1;
    PendingIntent pIntent;
    NotificationManager manager,manager1;
    Intent resultIntent;
    Button button,button1;
    EditText editText;
int a=0,b=100;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText= (EditText) findViewById(R.id.edittext);
        button= (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startNotification();
            }
        });
        button1= (Button) findViewById(R.id.button2);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Notifications();
            }
        });
    }

    public void buttonClick(View view)
    {
        startNotification();
    }

public void startNotification()
{
//       Intent intent=new Intent(MainActivity.this,MyService.class);
//       intent.putExtra("id",Integer.toString(a));
//    startService(intent);
//    PendingIntent pendingIntent=PendingIntent.getService(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);


    String str=editText.getText().toString();


    notification=new NotificationCompat.Builder(this);
    notification.setContentTitle("ABC");
    notification.setContentText(str);
    notification.setTicker("New Message Arrived");
    notification.setSmallIcon(R.mipmap.ic_launcher);
    notification.setGroup(GROUP_NOTIFICATIONS);
    notification.setGroupSummary(true);
    int num=0;

//    notification.setNumber(++num);


    Uri uri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    notification.setSound(uri);
    manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(a,notification.build());
    a++;
}

   public void Notifications()
   {
       String str=editText.getText().toString();


       notification1=new NotificationCompat.Builder(MainActivity.this);
       notification1.setContentTitle("ABC");
       notification1.setContentText(str);
       notification1.setTicker("New Message Arrived");
       notification1.setSmallIcon(R.mipmap.ic_launcher);
       notification1.setGroup(GROUP_NOTIFICATIONS);
       notification1.setGroupSummary(true);
       int num=0;

//       notification1.setNumber(++num);


       Uri uri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
       notification1.setSound(uri);
       manager1= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
       manager1.notify(a,notification1.build());
       a++;
   }
}

activity_main.xml(XML文件)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.pratik.stacked.MainActivity">


<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/edittext"
    android:textColor="@color/colorPrimary"
    android:textSize="20sp"/>


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Stacked Notification"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Stacked notifiction 1"
        android:id="@+id/button2"
        android:layout_above="@+id/button"
        android:layout_alignLeft="@+id/button"
        android:layout_alignStart="@+id/button"
        />
</RelativeLayout>

AndroidManifest.xml(清单文件)

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

2 个答案:

答案 0 :(得分:0)

问题是您正在递增(a++)通知ID:

manager1.notify(a,notification1.build());
a++;

如果要更新现有通知,则应使用要更新的通知ID调用notify。您可以参考the documentation

如果您使用的是Android&gt; = 4.1,则可以使用expanded layouts。请注意,在这种情况下,您可能希望确保these guidelines之后的兼容性。

答案 1 :(得分:0)

这就是设置正确的标志。 FLAG_GROUP_SUMMARY

相关问题