Android 推送通知中的优惠和交易实施
Offers and deals implementation in Push notifications in Android
我已经使用 fcm 实现了推送通知,但我希望通知类似于 Flipkart,如果我 post 向用户提供任何优惠和交易,则用户必须在单击时重定向到该页面在推送通知上。我该如何实施?
你好@Tirumalesh kallepalli 你可以通过使用 FirebaseMessagingService 中的 PendingIntent 来实现这个 class
示例
public class FcmListenerService extends FirebaseMessagingService {
private String msg;
NotificationHelper notificationHelper;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
JSONObject object = null;
try {
Map<String, String> params = remoteMessage.getData();
object = new JSONObject(params);
} catch (Exception e) {
}
sendNotification(object);
}
private void sendNotification(JSONObject object) {
//Here You can give your screen name whatever you want to open
//Instead of HomeActivity.class you can replace your activity
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
try {
msg = object.getString("message");
} catch (JSONException e) {
e.printStackTrace();
}
Uri defaultSoundUri =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_action_notification)
.setContentTitle("Firebase Push Notification")
.setContentText(msg)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
只要在 Pending Intent 中给出 activity 的名称即可,希望这对您有所帮助。
所以首先你需要一个独特的标签,例如。 notification_type 或在通知中收到的有效载荷内键入等,此类型用于将 PendingIntent 定向到它应该在的位置 redirected.So 假设您熟悉 FirebaseMessagingService,您正在管理 [=39 内的通知=] 现在在 class 中扩展 FirebaseMessagingService
定义 PendingIntent
的对象 这里是步骤
private PendingIntent pendingIntent = null;
像这样处理通知
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See showChatNotification method below.
//Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
// Log.d(TAG, "From: " + remoteMessage.getFrom());
//Logger.d(TAG + " getData: " + remoteMessage.getData().toString());
//Logger.d(TAG + " getData: " + remoteMessage.getNotification().getBody());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
Map<String, String> data = remoteMessage.getData();
setNotification(data);
} else {
Log.e(TAG, "Notificaiton");
}
}
我在这里创建了方法 setNotification(data);
以在收到通知时执行事件
private void showOtherNotification(Map<String, String> data) {
Date now = new Date();
uniqueId = now.getTime();
if (data.containsKey("type")) {
switch (data.get("type")) {
case "today_diss":
navigetToTodaysDisFragment(data);
break;
case "questionnaire":
navigetToQuestionaire(data);
break;
default:
navigateToHome();
break;
}
} else {
navigateToHome();
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder;
//Large Icon
Bitmap remote_picture = null;
try {
if (data.containsKey("image")) {
if (!data.get("image").isEmpty()) {
remote_picture = BitmapFactory.decodeStream(
(InputStream) new URL(data.get("image")).getContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
// this is a my insertion looking for a solution
int icon = getNotificationIcon();
if (data.containsKey("image") && remote_picture != null) {
notificationBuilder = new NotificationCompat.Builder(this,"default")
.setSmallIcon(icon)
.setContentTitle(getResources().getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(StringEscapeUtils.unescapeJava(data.get("message"))))
.setContentText(StringEscapeUtils.unescapeJava(data.get("message")))
.setAutoCancel(true)
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_MAX)
.setLargeIcon(remote_picture)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(remote_picture)
.setBigContentTitle(getString(R.string.app_name))
.setSummaryText(StringEscapeUtils.unescapeJava(data.get("message"))));
} else {
notificationBuilder = new NotificationCompat.Builder(this,"default")
.setSmallIcon(icon)
.setContentTitle(getResources().getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(StringEscapeUtils.unescapeJava(data.get("message"))))
.setContentText(StringEscapeUtils.unescapeJava(data.get("message")))
.setAutoCancel(true)
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_MAX);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) uniqueId, notificationBuilder.build());
}
如果您的通知包含可以在通知栏上显示的图像,我还为您提供管理丰富通知的解决方案
现在,如果你能看到代码的和平
case "today_diss":
navigetToTodaysDisFragment(data);
break;
case "questionnaire":
navigetToQuestionaire(data);
break;
default:
navigateToHome();
break;
这是您需要的主要解决方案,此方法也会将您重定向到您要重定向的屏幕,因此我在这里给您举个例子 navigateToHome();
此方法会将您重定向到主屏幕
private void navigateToHome() {
Intent intentToEventPage = new Intent(MyFirebaseMessagingService.this, DashActivity.class);
intentToEventPage.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentToEventPage.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
pendingIntent = PendingIntent.getActivity(this, (int) uniqueId /* Request code */, intentToEventPage, PendingIntent.FLAG_ONE_SHOT);
}
这样您可以根据您的要求修改 pendingIntent
以重定向到所需的 activity 或视图
我已经使用 fcm 实现了推送通知,但我希望通知类似于 Flipkart,如果我 post 向用户提供任何优惠和交易,则用户必须在单击时重定向到该页面在推送通知上。我该如何实施?
你好@Tirumalesh kallepalli 你可以通过使用 FirebaseMessagingService 中的 PendingIntent 来实现这个 class
示例
public class FcmListenerService extends FirebaseMessagingService {
private String msg;
NotificationHelper notificationHelper;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
JSONObject object = null;
try {
Map<String, String> params = remoteMessage.getData();
object = new JSONObject(params);
} catch (Exception e) {
}
sendNotification(object);
}
private void sendNotification(JSONObject object) {
//Here You can give your screen name whatever you want to open
//Instead of HomeActivity.class you can replace your activity
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
try {
msg = object.getString("message");
} catch (JSONException e) {
e.printStackTrace();
}
Uri defaultSoundUri =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_action_notification)
.setContentTitle("Firebase Push Notification")
.setContentText(msg)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
只要在 Pending Intent 中给出 activity 的名称即可,希望这对您有所帮助。
所以首先你需要一个独特的标签,例如。 notification_type 或在通知中收到的有效载荷内键入等,此类型用于将 PendingIntent 定向到它应该在的位置 redirected.So 假设您熟悉 FirebaseMessagingService,您正在管理 [=39 内的通知=] 现在在 class 中扩展 FirebaseMessagingService
定义 PendingIntent
的对象 这里是步骤
private PendingIntent pendingIntent = null;
像这样处理通知
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See showChatNotification method below.
//Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
// Log.d(TAG, "From: " + remoteMessage.getFrom());
//Logger.d(TAG + " getData: " + remoteMessage.getData().toString());
//Logger.d(TAG + " getData: " + remoteMessage.getNotification().getBody());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
Map<String, String> data = remoteMessage.getData();
setNotification(data);
} else {
Log.e(TAG, "Notificaiton");
}
}
我在这里创建了方法 setNotification(data);
以在收到通知时执行事件
private void showOtherNotification(Map<String, String> data) {
Date now = new Date();
uniqueId = now.getTime();
if (data.containsKey("type")) {
switch (data.get("type")) {
case "today_diss":
navigetToTodaysDisFragment(data);
break;
case "questionnaire":
navigetToQuestionaire(data);
break;
default:
navigateToHome();
break;
}
} else {
navigateToHome();
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder;
//Large Icon
Bitmap remote_picture = null;
try {
if (data.containsKey("image")) {
if (!data.get("image").isEmpty()) {
remote_picture = BitmapFactory.decodeStream(
(InputStream) new URL(data.get("image")).getContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
// this is a my insertion looking for a solution
int icon = getNotificationIcon();
if (data.containsKey("image") && remote_picture != null) {
notificationBuilder = new NotificationCompat.Builder(this,"default")
.setSmallIcon(icon)
.setContentTitle(getResources().getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(StringEscapeUtils.unescapeJava(data.get("message"))))
.setContentText(StringEscapeUtils.unescapeJava(data.get("message")))
.setAutoCancel(true)
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_MAX)
.setLargeIcon(remote_picture)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(remote_picture)
.setBigContentTitle(getString(R.string.app_name))
.setSummaryText(StringEscapeUtils.unescapeJava(data.get("message"))));
} else {
notificationBuilder = new NotificationCompat.Builder(this,"default")
.setSmallIcon(icon)
.setContentTitle(getResources().getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(StringEscapeUtils.unescapeJava(data.get("message"))))
.setContentText(StringEscapeUtils.unescapeJava(data.get("message")))
.setAutoCancel(true)
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_MAX);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) uniqueId, notificationBuilder.build());
}
如果您的通知包含可以在通知栏上显示的图像,我还为您提供管理丰富通知的解决方案
现在,如果你能看到代码的和平
case "today_diss":
navigetToTodaysDisFragment(data);
break;
case "questionnaire":
navigetToQuestionaire(data);
break;
default:
navigateToHome();
break;
这是您需要的主要解决方案,此方法也会将您重定向到您要重定向的屏幕,因此我在这里给您举个例子 navigateToHome();
此方法会将您重定向到主屏幕
private void navigateToHome() {
Intent intentToEventPage = new Intent(MyFirebaseMessagingService.this, DashActivity.class);
intentToEventPage.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentToEventPage.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
pendingIntent = PendingIntent.getActivity(this, (int) uniqueId /* Request code */, intentToEventPage, PendingIntent.FLAG_ONE_SHOT);
}
这样您可以根据您的要求修改 pendingIntent
以重定向到所需的 activity 或视图