【问题标题】:How to set onClick on Notification(Firebase) [duplicate]如何设置 onClick 通知(Firebase)[重复]
【发布时间】:2017-12-04 08:15:21
【问题描述】:

我想在通知上设置 onclick 以转到我从后端获得的特定活动。(FireBase)。目前,当我单击通知时,它会打开应用程序启动器活动。

【问题讨论】:

  • 不适合我

标签: android firebase firebase-cloud-messaging android-notifications


【解决方案1】:

请检查这是 MyFirebaseMessagingService 文件,我在这里设置了 onclick 并打开不同的活动:-

  • 这里在第一个“if (json instanceof JSONObject) {”条件下从通知中获得的 json 响应中获取响应,然后打开评论活动。

  • 在“else”中收到简单的消息并显示在通知上并从启动画面重新启动应用程序。

    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());
                }
            }
        }
    

希望对你有帮助。

【讨论】:

    【解决方案2】:

    如果您从后端发送{"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());
        }
    

    Main Activity 将使用作为 putExtra 传递的参数启动,然后您可以从那里根据参数路由到相应的 Activity。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2015-06-09
      • 1970-01-01
      相关资源
      最近更新 更多