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)。它帮助到我。通过这种方式,您的信息将始终得到扩展,并显示大文本。希望它能帮助你。