2016-12-13 68 views
-1

我試圖在Android中使用Xamarin.Android創建自定義通知。我創建的通知不會在不同的設備上顯示相同的通知。這是我正在使用的代碼:自定義通知Android在不同設備上顯示不一樣

Notification.Buider not = new Notification.Builder(this) 
    .SetTicker("Service started") 
    .SetContentTitle("This is the title") 
    .SetContentText("This is the content") 
    .SetSmallIcon(Resource.Drawable.Icon); 

Notification notification = not.Build(); 
notification.BigContentView=rv; 
NotificationManager manager = GetSystemService(Context.NotificationService) as NotificationManager; 
manager.Notify(0,notification); 

如果我在聯想A319(其運行Android 4.4.2)的通知是顯示不膨脹的rv變量,而如果我上運行的應用程序運行此Samsung Galaxy S7(Android 6.0.1)通知顯示爲rv變量中的佈局正在描述它。

我想知道爲什麼通知在不同的設備上顯示不一樣。它是由android版本引起的還是有我的代碼有問題?

Lenovo Android 4.4.2 enter image description here

+0

能告訴你的通知看起來像在不同的設備? –

+0

我編輯了這個問題。 –

+0

哪一個是你的通知?我在第一張圖片上看到「這是標題」,但在第二張圖片上? –

回答

0

我沒有你的CustomLayout一個片段,所以我創建了我的 「oneimg_twolbl.xml」 2個TextViews和1 ImageView的

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
     android:text="Text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/text_list4_view" /> 
    <TextView 
     android:text="Text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/text_list4_view2" /> 
    <ImageView 
     android:src="@android:drawable/ic_menu_gallery" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/image_list4_view" /> 
</LinearLayout> 

那麼我告訴這是自定義通知通過

 /////////////////////// custom notification ///////////////////////////////////// 
     RemoteViews v = new RemoteViews(PackageName, Resource.Layout.oneimg_twolbl); 
     v.SetTextViewText(Resource.Id.text_list4_view, "Text 1"); 
     v.SetTextViewText(Resource.Id.text_list4_view2, "Text 2"); 
     v.SetImageViewResource(Resource.Id.image_list4_view, Resource.Drawable.Icon); 

     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(this) 
       .SetSmallIcon(Resource.Drawable.Icon) 
       .SetCustomBigContentView(v) //if API >= 16 
       .SetContent(v) 
       .SetAutoCancel(true); 

     /////////////////////// custom notification to activity call ///////////////////////////////////// 

     // Creates an explicit intent for an Activity in your app 
     Intent resultIntent = new Intent(this, this.Class); 
     // The stack builder object will contain an artificial back stack for the 
     // started Activity. 
     // This ensures that navigating backward from the Activity leads out of 
     // your application to the Home screen. 
     Android.Support.V4.App.TaskStackBuilder stackBuilder = Android.Support.V4.App.TaskStackBuilder.Create(this); 
     // Adds the back stack for the Intent (but not the Intent itself) 
     stackBuilder.AddParentStack(this.Class); 
     // Adds the Intent that starts the Activity to the top of the stack 
     stackBuilder.AddNextIntent(resultIntent); 
     PendingIntent resultPendingIntent = 
       stackBuilder.GetPendingIntent(
        0, 
        (int)PendingIntentFlags.UpdateCurrent 
       ); 
     mBuilder.SetContentIntent(resultPendingIntent); 
     /////////////////////// end custom notification to activity call ///////////////////////////////////// 

     NotificationManager mNotificationManager = 
      (NotificationManager)GetSystemService(NotificationService); 
     // count allows you to update the notification later on. 
     mNotificationManager.Notify(count++, mBuilder.Build()); 
     /////////////////////// end custom notification ///////////////////////////////////// 

你可以看起來更這裏https://developer.xamarin.com/guides/android/application_fundamentals/notifications/

enter image description here

+0

感謝這個例子!我也想出了爲什麼我的通知有不同的外觀。在Galaxy S7上,通知顯示在最上面,所以它出現在擴展視圖模式下,而在聯想上,通知顯示爲第三個,因此它不處於擴展視圖模式,因此它被限制在64dp的高度,並且不能充氣我的佈局文件,我認爲它是160dp。現在你知道如何強制通知始終是第一個嗎? –

+0

嘗試不調用SetContentView,但只調用SetCustomBigContentView –

+0

爲什麼?這兩者有什麼區別?我假定SetCustomBigContentView以擴展模式顯示通知(當通知位於通知面板的頂部,因此佈局可以以高於64dp的高度顯示),而SetContentView以標準形式顯示通知(高度不大於比64dp)。兩者之間是否有其他區別? –

0

首先,我會建議你寫實際的代碼,而不是一個屏幕截圖。

其次,這裏是如何通知用戶

private void notifyUserOfDBupdate() { 
    //Intents 
    Intent Pdf_view = new Intent(this, //class to throw the user when they hit on notification\\.class); 
    PendingIntent pdf_view = PendingIntent.getActivity(this, 0, Pdf_view, 0); 


    //Notification Manager 
    NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); 


    //The note 
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    Notification noti = new NotificationCompat.Builder(getApplicationContext()) 
      .setTicker("TickerTitle") 
      .setContentTitle("content title") 
      .setSound(soundUri) 
      .setContentText("content text") 
      .setContentIntent(pdf_view).getNotification(); 


    //Execution 
    noti.flags = Notification.FLAG_AUTO_CANCEL; 
    nm.notify(0, noti); 
} 
+0

是的,我知道這一點,但我如何做出自定義通知?我想設計自己的xml(或axml)文件,並將該文件作爲我的通知。 –

相關問題