我想在通知上设置onclick,以转到我从后端获取的特定活动。(FireBase)。目前,当我点击通知时,它打开应用程序启动器活动。
答案 0 :(得分:0)
请在此处检查 MyFirebaseMessagingService 文件我设置onclick并打开其他活动: -
首先是#34; if(json instanceof JSONObject){"条件得到来自通知的json响应的响应,然后打开评论活动。
在" else"获得简单的消息并在通知上显示并从Splash Screen重新启动应用程序。
public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = "MyFirebaseMsgService"; Context mContext; /** * Called when message is received. * * @param remoteMessage Object representing the message received from Firebase Cloud Messaging. */ // [START receive_message] @Override public void onMessageReceived(RemoteMessage remoteMessage) { // TODO(developer): Handle FCM messages here. Log.d(TAG, "From: " + remoteMessage.getFrom()); Log.d(TAG, "Notification Message getMessageId: " + remoteMessage.getMessageId()); Log.d(TAG, "Notification Message getMessage: " + remoteMessage.getData().get("message")); Log.d(TAG, "Notification Message getAction: " + remoteMessage.getData().get("action")); Log.d(TAG, "Notification Message ImageURL: " + remoteMessage.getData().get("imageUrl")); Log.d(TAG, "Notification Message from: " + remoteMessage.getData().get("from")); Log.d(TAG, "Notification Message google.message_id " + remoteMessage.getData().get("google.message_id")); mContext = getApplicationContext(); Log.d(TAG, "Message data payload: " + remoteMessage.getData()); String msg = remoteMessage.getData().get("message"); try { sendNotification(msg); } catch (JSONException e) { e.printStackTrace(); } } private void sendNotification(String message) throws JSONException { String commenterFname = "", commenterLname = "", commenterProfileImage = "", userID = "", postID = ""; Object json = new JSONTokener(message).nextValue(); if (json instanceof JSONObject) { try { JSONObject msg = new JSONObject(message); commenterFname = msg.getString(Constant.JSON_KEY.COM_FNAME); commenterLname = msg.getString(Constant.JSON_KEY.COM_LNAME); commenterProfileImage = msg.getString(Constant.JSON_KEY.COM_IMAGE); userID = msg.getString(Constant.JSON_KEY.USER_ID); postID = msg.getString(Constant.JSON_KEY.POST_ID); } catch (JSONException e) { e.printStackTrace(); } String msg = commenterFname + " " + commenterLname + " " + getResources().getString(R.string.commented_on_your_post); int id = (int) System.currentTimeMillis(); Intent notificationIntent = new Intent(this, CommentActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); notificationIntent.putExtra("postID", postID); notificationIntent.setAction(postID); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setContentTitle("App_name") .setContentText(msg) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { notificationBuilder.setSmallIcon(R.mipmap.ic_launcher); } else { notificationBuilder.setSmallIcon(R.mipmap.ic_launcher); } NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(id, notificationBuilder.build()); } else { int id = (int) System.currentTimeMillis(); Intent notificationIntent = new Intent(this, SplashActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setContentTitle("KhetiVadi") .setContentText(message) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { notificationBuilder.setSmallIcon(R.mipmap.ic_launcher); } else { notificationBuilder.setSmallIcon(R.mipmap.ic_launcher); } NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(id, notificationBuilder.build()); } } }
希望它可以帮助你。
答案 1 :(得分:0)
如果您从后端发送{"notification":""}
数据,android会自动在通知栏中显示通知(仅当您集成了FCM sdk时)。
如果你想控制通知,不要从后端发送通知对象,只发送数据对象(如果你从firebase控制台发送通知进行测试,通知对象将默认存在)
每当您发送没有通知对象的通知时,都会执行onMessageReceived()
方法
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String data=remoteMessage.getData();
//parse data and show notification
showNotification(parsedMessageData);
}
private void sendNotification(String message) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("showNotification", true);
intent.putExtra("params", params);
if (page != null && page.equals(VIEW_INDENT)) {
intent.putExtra("page", HomeNavigationController.DISPLAY_INDENT_TAG);
}else if(page != null && page.equals(PAYMENT_SUMMERY)){
intent.putExtra("page", HomeNavigationController.DISPLAY_PAYMENT_SUMMERY_TAG);
intent.putExtra("month",paymentParams.getMonth());
intent.putExtra("year",paymentParams.getYear());
intent.putExtra("slotId",paymentParams.getSlotId());
}
else {
intent.putExtra("page", HomeNavigationController.PRODUCT_LIST_TAG);
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.farm_taaza_logo)
.setContentTitle("Sample Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
主要活动将以传递的参数作为putExtra启动,然后您可以根据参数传递到相应的活动。