我通过引用this链接在Android中实现了轮播通知。 一切顺利,并能够按预期获取图像。问题是,根据我的要求,我需要根据单击的图像导航到不同的URL。但是,当我尝试实现此目标时,我只能导航到一个URL(例如:我有3个图像和3个URL。如果单击“第二个图像”,则需要导航至第二个URL,但是无论单击哪个图像,仅导航到第三个URL。)
下面是我的通知和JSON附带的代码。
public void setNotification(String notification,String title,Bitmap bitmap,Bitmap bitmapIcon,String uri,String WSC_ID,String fcmOperator,String fcmaccessToken,String User_Id,String push_UID,List<Carousel> carouselList,String display_type){
Intent urlIntent=null;
//notification Id
int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
RemoteViews bigView = new RemoteViews(context.getPackageName(),R.layout.customlayout);
//close button
Intent closeButtonIntent = new Intent(context,NotificationReceiver.class);
closeButtonIntent.setaction("CLOSEactION");
closeButtonIntent.putExtra("notificationId",m);
closeButtonIntent.putExtra("closeaction","close");
closeButtonIntent.putExtra("WSC_ID",WSC_ID);
closeButtonIntent.putExtra("access_Token",fcmaccessToken);
closeButtonIntent.putExtra("status",notificationStatus);
closeButtonIntent.putExtra("user_Id",User_Id);
closeButtonIntent.putExtra("operator",fcmOperator);
closeButtonIntent.putExtra("push_UID",push_UID);
PendingIntent dismissIntent = PendingIntent.getBroadcast(context,m,closeButtonIntent,PendingIntent.flaG_CANCEL_CURRENT);
bigView.setOnClickPendingIntent(R.id.crossmark,dismissIntent);
bigView.setTextViewText(R.id.titleTv,title);
bigView.setTextViewText(R.id.messageTv,notification);
bigView.setImageViewBitmap(R.id.smallIcon,bitmapIcon);
//for display_type = banner/text
if (display_type.equalsIgnoreCase("Banner") || display_type.equalsIgnoreCase("Text")){
bigView.setViewVisibility(R.id.viewFlipper,View.GONE);
//for display_type = banner
if (display_type.equalsIgnoreCase("Banner")){
bigView.setImageViewBitmap(R.id.bannerImageView,bitmap);
bigView.setViewVisibility(R.id.bannerImageView,View.VISIBLE);
}
urlIntent = new Intent(context,Office24by7Urlactivity.class);
urlIntent.putExtra("uri",uri);
urlIntent.putExtra("notificationId",m);
urlIntent.putExtra("urlaction","urlaction");
urlIntent.putExtra("WSC_ID",WSC_ID);
urlIntent.putExtra("access_Token",fcmaccessToken);
urlIntent.putExtra("status",notificationStatus);
urlIntent.putExtra("user_Id",User_Id);
urlIntent.putExtra("operator",fcmOperator);
urlIntent.putExtra("push_UID",push_UID);
PendingIntent nextIntent=PendingIntent.getactivity(context,urlIntent,PendingIntent.flaG_UPDATE_CURRENT);
bigView.setOnClickPendingIntent(R.id.customlayoutRl,nextIntent);
}
//for CarouselNotification
if (carouselList.size() > 0){
for (int i=0; i<carouselList.size(); i++){
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.image_layout);
URL url = null;
try {
url = new URL(carouselList.get(i).getImgs());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap1 = BitmapFactory.decodeStream(input);
bigView.setViewVisibility(R.id.viewFlipper,View.VISIBLE);
bigView.setViewVisibility(R.id.bannerImageView,View.GONE);
bigView.setDisplayedChild(R.id.viewFlipper,i+1);
remoteViews.setImageViewBitmap(R.id.largeIconIv,bitmap1);
Intent intent = new Intent(context,ViewFlipperWidgetProvider.class);
bigView.setRemoteAdapter(R.id.viewFlipper,intent);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//click events
urlIntent = new Intent(context,Office24by7Urlactivity.class);
urlIntent.putExtra("uri",carouselList.get(i).getImgurl());
urlIntent.putExtra("notificationId",m);
urlIntent.putExtra("urlaction","urlaction");
urlIntent.putExtra("WSC_ID",WSC_ID);
urlIntent.putExtra("access_Token",fcmaccessToken);
urlIntent.putExtra("status",notificationStatus);
urlIntent.putExtra("user_Id",User_Id);
urlIntent.putExtra("operator",fcmOperator);
urlIntent.putExtra("push_UID",push_UID);
PendingIntent nextIntent=PendingIntent.getactivity(context,PendingIntent.flaG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.largeIconIv,nextIntent);
// Adding each image view in the viewflipper.
bigView.addView(R.id.viewFlipper,remoteViews);
}
}
Notificationmanager mNotifyManager = (Notificationmanager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context);
foregroundNote = mNotifyBuilder.setContentTitle(title)
.setContentText(notification)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(bitmapIcon)
.setautoCancel(true)
.build();
foregroundNote.bigContentView = bigView;
assert mNotifyManager != null;
mNotifyManager.notify(m,foregroundNote);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
String channelId="Customlibrary";
NotificationChannel notificationChannel=new NotificationChannel(channelId,"Customlibrary Notification",Notificationmanager.IMPORTANCE_HIGH);
Notification testNotification=new Notification.Builder(context,channelId)
.setContentTitle(title)
.setContentText(notification)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(bitmapIcon)
.setCustomBigContentView(bigView)
.setautoCancel(true)
.build();
mNotifyManager.createNotificationChannel(notificationChannel);
mNotifyManager.notify(m,testNotification);
}
}//end of setNotification
我的通知布局(customLayout):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/customlayoutRl">
<ImageView
android:id="@+id/smallIcon"
android:layout_width="40dp"
android:layout_height="40dp"
android:gravity="center"/>
<TextView
android:id="@+id/titleTv"
android:maxLines="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/smallIcon"
android:layout_marginStart="5dp"
android:layout_marginTop="8dp"
android:hint="title" />
<TextView
android:id="@+id/messageTv"
android:layout_width="wrap_content"
android:maxLines="3"
android:layout_height="wrap_content"
android:layout_below="@+id/titleTv"
android:layout_toRightOf="@id/smallIcon"
android:layout_marginStart="5dp"
android:layout_marginTop="4dp"
android:hint="message" />
<ImageView
android:id="@+id/crossmark"
android:src="@drawable/crossmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp" />
<ImageView
android:id="@+id/bannerImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:layout_below="@+id/messageTv"
android:layout_marginBottom="15dp"
android:visibility="gone"/>
<ViewFlipper
android:id="@+id/viewFlipper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/messageTv"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:autoStart="true"
android:flipInterval="2500"
android:inAnimation="@android:anim/slide_out_right"
android:outAnimation="@android:anim/slide_in_left"
android:visibility="gone">
</ViewFlipper>
</RelativeLayout>
image_layout.xml:
<ImageView
android:id="@+id/largeIconIv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/crossmark"
xmlns:android="http://schemas.android.com/apk/res/android" />
请帮助我根据单击的图像导航到不同的URL。 在此先感谢。