android - FCM Android - 点击通知 - 打开网页视图
问题描述
我使用的模板:https ://github.com/mgks/Android-SmartWebView 实际上,该模板没有 fcm 功能。我手动添加的。我在这里提到,以便您查看 mainactivity 文件。
当用户单击我的通知时,我想打开特定链接。
我的通知生成器是:
// pending implicit intent to view url
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("LINK",link);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, ADMIN_CHANNEL_ID)
.setLargeIcon(BitmapFactory.decodeResource(getBaseContext().getResources(), R.mipmap.ic_launcher)) //set it in the notification
.setSmallIcon(R.mipmap.ic_launcher) //a resource for your custom small icon
.setContentTitle(title) //the "title" value you sent in your notification
.setContentText(message) //ditto
.setContentIntent(pendingIntent)
.setAutoCancel(true) //dismisses the notification on click
.setSound(defaultSoundUri)
.setStyle(new NotificationCompat.BigPictureStyle()
.setSummaryText(message)
.bigPicture(bitmap)
.setBigContentTitle(title));
我在 MainActivity 中试过这个:
Intent intent = getIntent();
if (getIntent().getExtras() != null && getIntent().getExtras().getString("link", null) != null && !getIntent().getExtras().getString("link", null).equals("")) {
String url = null;
if (getIntent().getExtras().getString("link").contains("http")) {
url = getIntent().getExtras().getString("link");
} else {
url = "http://" + getIntent().getExtras().getString("link");
}
aswm_view(url, false);
} else {
//Rendering the default URL
aswm_view(ASWV_URL, false);
}
(near lines 250)
但是,没有任何工作。谁能帮帮我吗?
谢谢你。
解决方案
我认为你应该使用intent.getString()
而不是intent.getExtras().getString()
:
Intent intent = getIntent();
String url = getIntent().getString("link", null)
if (url != null) {
if (!url.startsWith("http")) {
url = "http://" + url;
}
aswm_view(url, false);
} else {
//Rendering the default URL
aswm_view(ASWV_URL, false);
}
根据Intent.getExtras您intent.getExtras()
将返回null
,因为它从未被分配。
返回之前使用 putExtra() 添加的所有附加项的映射,如果没有添加则返回 null
推荐阅读
- typescript - 打字稿:可以用任意类型实例化
- scrapy - Scrapy startproject PermissionError
- python - Datamax DLP 使用 python 在其他文本上方发送和打印图像
- javascript - 为什么来自 AJAX 请求的响应数据出错而不成功?
- java - 在 Micronaut 中使用 Consul 集中配置
- php - 将 SimpleXMLElement 更改为漂亮的打印
- java - 使用 Spring Boot 应用程序出现 RabbitMQ 集群节点故障
- python - 如何从运行 Fargate ECS 任务中查看 Python 打印语句?
- python - 使用 Pandas 转换 NBA API 数据时出现回溯错误
- node.js - 找不到模块