FCM Android - 单击通知 - 打开 webview
FCM Android - On click Notification - Open webview
我使用的模板:https://github.com/mgks/Android-SmartWebView
实际上,该模板没有 fcm 功能。我手动添加的。我在此处引用,以便您查看 mainactivity 文件。
我想在用户点击我的通知时打开特定 link。
我的通知生成器是:
// 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()
会 return null
因为它从未被分配。
Returns the map of all extras previously added with putExtra(), or null if
none have been added
intent.putExtra("LINK",link);
String url = getIntent().getString("link", null)
这是我的错误。 (LINK & link 不同。
为了这个愚蠢的错误,我花了将近 1 周的时间。
我使用的模板:https://github.com/mgks/Android-SmartWebView 实际上,该模板没有 fcm 功能。我手动添加的。我在此处引用,以便您查看 mainactivity 文件。
我想在用户点击我的通知时打开特定 link。
我的通知生成器是:
// 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()
会 return null
因为它从未被分配。
Returns the map of all extras previously added with putExtra(), or null if none have been added
intent.putExtra("LINK",link);
String url = getIntent().getString("link", null)
这是我的错误。 (LINK & link 不同。
为了这个愚蠢的错误,我花了将近 1 周的时间。