0

setStylebigText函數無法正常工作。其實我想創建像flipkart亞馬遜或其他購物網站bigpicture通知。但它不工作,所以我只是試圖用簡單的代碼檢查setStyle的工作,但它也不起作用。Android notification-setStyle或bigText功能不能正常工作

注意:我沒有在xml(內部res)文件中做過任何配置..請讓我知道是否有任何一些額外的配置或需求的要求。我剛剛創建了MainActivity.java文件,並在下面編寫了通知代碼。

MainActivity.java

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.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.app.NotificationCompat; 
import android.view.View; 
import android.widget.Button; 

import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.appindexing.Thing; 
import com.google.android.gms.common.api.GoogleApiClient; 

public class MainActivity extends AppCompatActivity { 
    Button b1; 
    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     b1 = (Button) findViewById(R.id.button); 
     b1.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       notificationinfo(); 
      } 
     }); 
     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 

    } 
    public void notificationinfo() 
    { 
     Intent intent = new Intent(this,MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle("test") 
       .setStyle(new NotificationCompat.BigTextStyle().bigText("body")) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 
     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0, notificationBuilder.build()); 
    } 

    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    public Action getIndexApiAction() { 
     Thing object = new Thing.Builder() 
       .setName("Main Page") // TODO: Define a title for the content shown. 
       // TODO: Make sure this auto-generated URL is correct. 
       .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]")) 
       .build(); 
     return new Action.Builder(Action.TYPE_VIEW) 
       .setObject(object) 
       .setActionStatus(Action.STATUS_TYPE_COMPLETED) 
       .build(); 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client.connect(); 
     AppIndex.AppIndexApi.start(client, getIndexApiAction()); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     AppIndex.AppIndexApi.end(client, getIndexApiAction()); 
     client.disconnect(); 
    } 
} 
+0

在您的通知中不能正常工作不會發布或其他內容? – ianhanniballake

+0

它被張貼..但只有標題出現bigText包含不出現..same事情正在與bigPictute功能發生 –

+0

你真的擴大通知?只有最上面的通知是自動展開的 - 您需要用兩隻手指輕掃任何其他通知才能展開它們。 – ianhanniballake

回答

0

我不知道,我在時間。但我知道,如果您不以這種方式將簡單的文本添加到您的通知(setContentText(text))中,您將看不到任何內容,不包括您的標題。我明白你想要什麼。但是你需要拖動你的通知消息。但我不確定這個功能是否適用於所有的Android版本。

而且,最後一個小技巧可以幫助你!您可以爲您的notificationBuilder添加優先級。只需做setPriority(Notification.PRIORITY_MAX)。它幫助到我。通過這種方式,您的信息將始終得到擴展,並顯示大文本。希望它能幫助你。