firebase fcm_使用Firebase Cloud Messaging(FCM)的Android推送通知

firebase fcm

In this tutorial you will learn to implement android push notification using firebase cloud messaging (FCM).

在本教程中,您将学习使用Firebase云消息传递(FCM)实现android推送通知。

Firebase is a real time cross platform database that provides various functionalities like authentication, storage, notification, etc. Firebase is becoming popular among developers due to its simplicity and easy implementation. In earlier days Google Cloud Messaging (GCM) was used to implement push notification. But Firebase Cloud Messaging (FCM) is better and easy to implement than GCM.

Firebase是一个实时的跨平台数据库,提供了各种功能,如身份验证,存储,通知等。Firebase由于其简单性和易于实现而在开发人员中变得越来越流行。 早期,Google Cloud Messaging(GCM)用于实现推送通知。 但是Firebase Cloud Messaging(FCM)比GCM更好,更容易实现。

firebase fcm_使用Firebase Cloud Messaging(FCM)的Android推送通知

使用Firebase Cloud Messaging(FCM)的Android推送通知 (Android Push Notification Using Firebase Cloud Messaging (FCM))

先决条件 (Prerequisites)

To use FCM there are few minimum requirements.

要使用FCM,几乎没有最低要求。

  • Device with Android 2.3 or newer and Google Play services 9.6.0 or newer

    具有Android 2.3或更高版本以及Google Play服务9.6.0或更高版本的设备

  • The Google Repository from the Android SDK Manager

    Android SDK Manager中的Google Repository

  • Android Studio 1.5 or higher version.

    Android Studio 1.5或更高版本。

Android专案 (Android Project)

Create a new android studio project with package name com.pushnotificationexample and copy the package name.

使用包名称com.pushnotificationexample创建一个新的android studio项目,并复制包名称。

Firebase项目 (Firebase Project)

Go to Firebase console https://firebase.google.com/console/ to create a new project.

转到Firebase控制台https://firebase.google.com/console/创建一个新项目。

Click on Create New Project button. Enter some project name and your country. Click on Create Project button to create the firebase project.

单击创建新项目按钮。 输入一些项目名称和您所在的国家。 单击“ 创建项目”按钮以创建firebase项目。

Choose Add Firebase to your Android app option.

选择将Firebase添加到您的Android应用程序选项。

Enter the package name that you copied earlier and click Add App button. You will get a google-services.json file.

输入您先前复制的程序包名称,然后单击“ 添加应用程序”按钮。 您将获得google-services.json文件。

将Firebase添加到Android项目 (Add Firebase to Android Project)

Select Project view in Android Studio and paste the google-services.json file under app folder.

在Android Studio中选择“ 项目”视图,然后将google-services.json文件粘贴到app文件夹下。

Open project level build.gradle file and add following line under dependencies.

打开项目级别的build.gradle文件,并在依赖项下添加以下行。

1
classpath 'com.google.gms:google-services:3.0.0'
1
classpath 'com.google.gms:google-services:3.0.0'

Open app level build.gradle file and add following line under dependencies.

打开应用程序级别的build.gradle文件,并在依赖项下添加以下行。

1
compile 'com.google.firebase:firebase-core:9.6.0'
1
compile 'com.google.firebase:firebase-core:9.6.0'

Also add following line at the end of app level build.gradle file.

还要在应用程序级别build.gradle文件的末尾添加以下行。

1
apply plugin: 'com.google.gms.google-services'
1
apply plugin : 'com.google.gms.google-services'

Firebase云消息传递(FCM)实施 (Firebase Cloud Messaging (FCM) Implementation)

Create a new class MyFirebaseMessagingService that extends FirebaseMessagingService. Add following code inside it.

创建一个新类MyFirebaseMessagingService ,以扩展FirebaseMessagingService 。 在其中添加以下代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.firebasepushnotification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //calling method to generate push notification
        sendNotification(remoteMessage.getNotification().getBody());
    }
    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Push Notification Example")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com . firebasepushnotification ;
import android . app . NotificationManager ;
import android . app . PendingIntent ;
import android . content . Context ;
import android . content . Intent ;
import android . media . RingtoneManager ;
import android . net . Uri ;
import android . support . v4 . app . NotificationCompat ;
import com . google . firebase . messaging . FirebaseMessagingService ;
import com . google . firebase . messaging . RemoteMessage ;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
     @Override
     public void onMessageReceived ( RemoteMessage remoteMessage ) {
         //calling method to generate push notification
         sendNotification ( remoteMessage . getNotification ( ) . getBody ( ) ) ;
     }
     private void sendNotification ( String messageBody ) {
         Intent intent = new Intent ( this , MainActivity . class ) ;
         intent . addFlags ( Intent . FLAG_ACTIVITY_CLEAR_TOP ) ;
         PendingIntent pendingIntent = PendingIntent . getActivity ( this , 0 , intent ,
                 PendingIntent . FLAG_ONE_SHOT ) ;
         Uri defaultSoundUri = RingtoneManager . getDefaultUri ( RingtoneManager . TYPE_NOTIFICATION ) ;
         NotificationCompat . Builder notificationBuilder = new NotificationCompat . Builder ( this )
                 . setSmallIcon ( R . mipmap . ic_launcher )
                 . setContentTitle ( "Push Notification Example" )
                 . setContentText ( messageBody )
                 . setAutoCancel ( true )
                 . setSound ( defaultSoundUri )
                 . setContentIntent ( pendingIntent ) ;
         NotificationManager notificationManager =
                 ( NotificationManager ) getSystemService ( Context . NOTIFICATION_SERVICE ) ;
         notificationManager . notify ( 0 , notificationBuilder . build ( ) ) ;
     }
}

Now we have to add above service in AndroidManifest.xml. So add following code anywhere between <application> <application/> tag.

现在,我们必须在AndroidManifest.xml中添加上述服务。 因此,在<application> <application />标记之间的任何位置添加以下代码。

1
2
3
4
5
6
<service
    android:name=".MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>
1
2
3
4
5
6
< service
    android : name = ".MyFirebaseMessagingService" >
    < intent - filter >
        < action android : name = "com.google.firebase.MESSAGING_EVENT" / >
    < / intent - filter >
< / service >

Build and run your app.

生成并运行您的应用程序。

发送推送通知 (Sending Push Notification)

Go back to Firebase Console and select Notifications option from left menu. Click on New Message button.

返回Firebase控制台,然后从左侧菜单中选择“ 通知”选项。 单击新消息按钮。

Add a message in Message text box. Choose Target as User Segment. This will send the notification to all the users using this android app. You can also send notification to a particular user’s device.

消息文本框中添加一条消息。 选择目标作为用户群 。 这会将通知发送给使用此android应用的所有用户。 您还可以将通知发送到特定用户的设备。

Choose the app package name from drop down menu. Finally click on Send Message button to send the message.

从下拉菜单中选择应用程序包名称。 最后单击发送消息按钮发送消息。

firebase fcm_使用Firebase Cloud Messaging(FCM)的Android推送通知
firebase fcm_使用Firebase Cloud Messaging(FCM)的Android推送通知

If you have done everything correctly then you will see a push notification in your device.

如果您已正确完成所有操作,则设备中将显示一个推送通知。

Comment below if you are facing any problem in above android push notification tutorial.

如果您在上面的Android推送通知教程中遇到任何问题,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2016/10/android-push-notification-using-firebase-cloud-messaging-fcm.html

firebase fcm