3分钟实现app推送开发

为了在应用程序中实现推送功能,我们需要为应用程序添加推送通知服务。推送通知服务允许开发人员通过服务器发送通知消息给客户端设备 (如 Android、iOS 和 Web 应用程序)。使用推送通知服务实现推送功能可以帮助应用程序实时更新用户关心的信息。在本篇文章中,我们将通过 Google Firebase 为 Android 应用程序添加推送通知服务。

Firebase 是 Google 提供的移动应用程序开发平台,提供多种云服务,如疾风推送、数据库、分析等。Firebase Android SDK 提供了一个称为 Firebase Cloud Messaging (FCM) 的推送通知服务,因此我们将使用 FCM 实现推送通知功能。

步骤1:创建 Firebase 项目

首先,我们需要创建一个 Firebase 项目,以便我们可以使用 Firebase 服务。

1.1 转到 Firebase Console。

1.2 单击“新建项目”按钮。

1.3 输入项目名称,选择项目所在的国家/地区,然后单击“继续”。

1.4 启用 Google Analytics 并单击创建项目。

步骤2:添加 Firebase 到 Android 应用程序

为了在 Android 应用程序中使用 Firebase 服务,我们需要将 Firebase 添加到 Android 项目中。这可以使用 Firebase Assistant 完成。

2.1 打开 Android Studio。

2.2 单击菜单栏中的“Tools”并单击“Firebase”。

2.3 在 Firebase Assistant 中,单击“云消息传递”。

2.4 单击“为应用程序添加 Firebase Cloud Messaging”。

2.5 选择您的应用程序,并单击“继续”。

2.6 根据说明在项目中添加 Firebase SDK。

2.7 单击“完成”并等待您的应用程序重新构建。

步骤3:配置 Firebase Cloud Messaging

现在我们已经添加了 Firebase 到我们的应用程序中,我们需要配置 Firebase Cloud Messaging 功能。

3.1 转到 Firebase Console 中的 “设置” → “云消息传递”。

3.2 在“Android 应用程序”标签下,单击“添加应用程序”。

3.3 输入应用程序的包名并单击“注册应用程序”。

3.4 下载并保存 `google-services.json` 配置文件。

3.5 将 `google-services.json` 配置文件复制到您的项目根目录的 /app 目录下。

3.6 在 项目 build.gradle 文件中添加以下依赖项:

```

dependencies {

// ...

implementation 'com.google.firebase:firebase-messaging:20.0.1'

}

```

步骤4:实现推送消息

已经准备好了,现在让我们开始推送消息。我们将在 `FirebaseMessagingService` 类中实现推送消息功能。将以下代码添加到您的应用程序的 `FirebaseMessagingService` 类:

```

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMessagingService";

@Override

public void onMessageReceived(RemoteMessage remoteMessage) {

// ...

// 推送标题

String notificationTitle = remoteMessage.getNotification().getTitle();

// 推送消息

String notificationMessage = remoteMessage.getNotification().getBody();

// 唯一 ID 用于清除通知

int notificationId = (int) System.currentTimeMillis();

// 创建通知

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")

.setContentTitle(notificationTitle)

.setContentText(notificationMessage)

.setPriority(NotificationCompat.PRIORITY_HIGH)

.setSmallIcon(R.drawable.notification_icon)

.setAutoCancel(true);

// 显示通知

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

notificationManager.notify(notificationId, notificationBuilder.build());

}

}

```

代码解释:

1. `onMessageReceived`:在接收到推送消息时调用该方法。

2. `remoteMessage.getNotification().getTitle()`:获取推送标题。

3. `remoteMessage.getNotification().getBody()`:获取推送消息。

4. `System.currentTimeMillis()`:获取当前时间戳,用于生成唯一的通知 ID。

5. 使用 `NotificationCompat.Builder` 创建通知。

6. 使用 `NotificationManagerCompat` 显示通知。

步骤5:测试推送消息

我们已经完成了所有步骤。现在我们可以通过 Firebase Console 发送测试通知,Screenshot:

![image.png](https://cdn.nlark.com/yuque/0/2021/png/242221/1631061553450-2aee6c9d-a17e-45ce-8201-057b74a13264.png)

结束语

在本文中,我们学习了如何通过 Firebase Cloud Messaging 实现 Android 应用程序的推送通知功能。我们创建了 Firebase 项目、将 Firebase 添加到 Android 应用程序中、配置 Firebase Cloud Messaging,实践了推送消息代码。通过本文的步骤,您可以为您的应用程序添加推送功能,并实时发送通知给您的用户。

川公网安备 51019002001185号