gpt4 book ai didi

android - 如何根据通知中点击的图像导航到不同的 URL?

转载 作者:行者123 更新时间:2023-11-30 04:57:33 29 4
gpt4 key购买 nike

我引用this在android中实现了Carousel Notifications|关联。一切顺利,能够按预期获取图像。问题是,根据我的要求,我需要根据点击的图像导航到不同的 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,m,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,m,urlIntent,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>

图像布局.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。提前致谢..

最佳答案

终于,我得到了答案。根据我的要求,最多只显示 5 张图像,最少显示一张。因此,我在 for 循环 中使用了一个 switch 案例并获得了所需的输出。我希望我的代码对寻找相同场景的其他人有所帮助。

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;

//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,m,urlIntent,PendingIntent.FLAG_UPDATE_CURRENT);
bigView.setOnClickPendingIntent(R.id.customlayoutRl, nextIntent);
}

//for CarouselNotification
//If CarouselNotification is Empty
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);


remoteViews.setImageViewBitmap(R.id.largeIconIv, bitmap1);

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(TAG, "setNotification: "+i);
//click events
urlIntent = new Intent(context, Office24by7UrlActivity.class);
switch (i){
case 0:
urlIntent.putExtra("uri", carouselList.get(0).getImgurl());
break;
case 1:
urlIntent.putExtra("uri", carouselList.get(1).getImgurl());
break;
case 2:
urlIntent.putExtra("uri", carouselList.get(2).getImgurl());
break;
case 3:
urlIntent.putExtra("uri", carouselList.get(3).getImgurl());
break;
case 4:
urlIntent.putExtra("uri", carouselList.get(4).getImgurl());
break;

}

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,i,urlIntent,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);

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);
}else{
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);

}
}//end of setNotification

特别感谢 Mike,他的回复非常迅速..

关于android - 如何根据通知中点击的图像导航到不同的 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58874382/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com