2016-06-21 66 views
0

我有一個簡單的應用程序,它會在按鈕上生成通知單擊。我也有一個editText,以便用戶鍵入內容文本。我有兩個按鈕可以生成兩個不同的通知。我的要求是,每按一下按鈕,應該只生成一個通知,其餘的應該在通知堆棧中。我曾嘗試使用通知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> 

回答

0

問題是你a再遞增(a++)通知ID:

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

如果您想更新現有通知你應該打電話與您要更新的通知ID通知。您可以參考the documentation

如果您使用Android> = 4.1,則可以使用expanded layouts。考慮到在這種情況下,您可能需要確保遵循these guidelines的兼容性。

+0

問題是關於分組通知,而不是? –

+0

我已經試過了。在這種情況下,不是將通知分組,而是用新的通知替換當前的通知。 –

+0

我已更新我的回答,指出Android> = 4.1的通知的「擴展布局」選項 – antonio