使用Firebase发送邀请通知-Android

时间:2019-07-18 17:17:00

标签: android google-cloud-firestore

我正在创建一个游戏。在一个活动中,有2个按钮:开始游戏按钮和加入游戏按钮。

点击开始游戏按钮时,该应用会打开包含所有可用用户的活动。当单击用户时,会出现一个对话框,询问用户是否要向单击的用户发送邀请。如果是,将随机创建一个6位代码并将其添加到数据库中。然后,单击的用户将收到该邀请的通知。如果应用程序在后台运行,则通知将显示在应用程序内部。如果没有,它将在通知栏中显示为正常通知。

单击加入游戏按钮时,将出现一个包含EditText的对话框,要求输入代码。如果代码在Firestore中存在,则游戏将开始。否则,将显示错误消息。

我正在尝试执行此操作,因为我不知道如何执行操作。

无论如何,如果游戏开始,这就是我尝试的目的:

holder.view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (userID.equals(FirebaseAuth.getInstance().getCurrentUser().getDisplayName())) {

                Toast.makeText(context, "You can't play with yourself", Toast.LENGTH_SHORT).show();

            } else {

                final AlertDialog.Builder gameInviteBuilder = new AlertDialog.Builder(context);
                gameInviteBuilder.setTitle("Would you like to invite " + username + " to a game?");
                gameInviteBuilder.setPositiveButton("SURE!", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        char[] chars1 = "0123456789".toCharArray();

                        StringBuilder stringBuilder = new StringBuilder();
                        Random random = new Random();
                        for (int i = 0; i < 6; i++) {
                            char c1 = chars1[random.nextInt(chars1.length)];
                            stringBuilder.append(c1);
                        }
                        final String gameID = stringBuilder.toString();

                        final String userUsername = firebaseUser.getDisplayName();

                        final Map<String, String> gameMap = new HashMap<>();
                        gameMap.put("id", gameID);
                        gameMap.put("player", userUsername);
                        gameMap.put("playing with", username);

                        firebaseFirestore.collection("Games").document(gameID).set(gameMap).addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {

                                firebaseFirestore.collection("Users").document(username).collection("Games").document(gameID).set(gameMap);
                                firebaseFirestore.collection("Users").document(userUsername).collection("Games").document(gameID).set(gameMap);

                                if (!username.equals(userUsername)) {

                                    sendNotification();

                                }

                            }

                            private void sendNotification() {

                                Intent notificationInviteGameIntent = new Intent(context, GameActivity.class);
                                PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationInviteGameIntent, 0);

                                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "1");
                                notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
                                notificationBuilder.setContentTitle("Ensan 7ayawan Shay2");
                                notificationBuilder.setContentText(username + " has invited you to a game");
                                notificationBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
                                notificationBuilder.setContentIntent(pendingIntent);
                                notificationBuilder.setAutoCancel(true);

                                NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
                                // notificationId is a unique int for each notification that you must define
                                notificationManager.notify(Integer.parseInt(gameID), notificationBuilder.build());

                            }
                        });

                    }
                });
                gameInviteBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();

                    }
                });

                AlertDialog alertDialog = gameInviteBuilder.create();
                alertDialog.show();

            }

        }
    });

在if循环中添加 sendNotification()方法时出现错误。这不是一种方法。但是我做到了,所以我没有出错。

这是用于加入游戏按钮的:

joinGameButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            LayoutInflater inflater = getLayoutInflater();

            final EditText codeText = new EditText(GameChooserActivity.this);

            AlertDialog.Builder joinGameDialogBuilder = new AlertDialog.Builder(GameChooserActivity.this);
            joinGameDialogBuilder.setTitle("Enter Code");
            joinGameDialogBuilder.setView(inflater.inflate(R.layout.join_game_enter_code, null));
            joinGameDialogBuilder.setPositiveButton("Join", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    final String code = codeText.getText().toString();

                    if (!TextUtils.isEmpty(code)) {

                        firebaseFirestore.collection("Games Codes").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                            @Override
                            public void onComplete(@NonNull Task<QuerySnapshot> task) {

                                if (task.isSuccessful()) {

                                    for (DocumentSnapshot documentSnapshot : task.getResult()) {

                                        if (documentSnapshot.exists()) {

                                            final String gameID = documentSnapshot.getString("id");

                                            firebaseFirestore.collection("Users").document(gameID).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                                                @Override
                                                public void onSuccess(DocumentSnapshot documentSnapshot) {

                                                    String playingWith = documentSnapshot.getString("playing with");

                                                    if (gameID.equals(code)) {

                                                        Intent gameIntent = new Intent(GameChooserActivity.this, GameActivity.class);
                                                        gameIntent.putExtra("game_user_username", playingWith);
                                                        startActivity(gameIntent);

                                                    }

                                                }
                                            });

                                        }

                                    }

                                }

                            }
                        });

                    }

                }
            });
            joinGameDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();

                }
            });

            AlertDialog joinGameDialog = joinGameDialogBuilder.create();
            joinGameDialog.show();


        }
    });

请帮忙吗? :)

0 个答案:

没有答案