自定义通知Android在不同设备上不会显示相同的内容

时间:2016-12-13 13:57:47

标签: android xamarin notifications

我尝试使用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

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:0)

我没有你的CustomLayout,所以我用2个TextViews和1个ImageView创建了我的“oneimg_twolbl.xml”

<?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