firebase 云通知功能使用。有独立的demo可用,还有测试工具。

资源在:https://download.csdn.net/download/qq_38998213/10478112。这里面包括项目 和测试工具,想要的私聊我就行,下载还得1积分,设的最低,感觉应该不设这个东西。

1.首先你的有firebase账号,进行登录,一般用gmail邮箱进行注册。网址如下:https://console.firebase.google.com/

2.添加你的应用到Firebase,需要你的包名等,如下图:firebase 云通知功能使用。有独立的demo可用,还有测试工具。

最后你会得到一个google-services.json文件,复制到app目录中。

3.在android studio中 与firebase进行连接,点击Android studio 的Tools,Firebase,会打开一个Firebase窗口:点Clude Messageing 目录心爱的set up如下图:

firebase 云通知功能使用。有独立的demo可用,还有测试工具。

按照步骤123:,进行操作,如下图:

firebase 云通知功能使用。有独立的demo可用,还有测试工具。

在连接时需要你写两个服务,在Firebase发出消息时你能接到消息,类我已经写好了,直接就能用:

首先你导包:

compile 'com.google.firebase:firebase-config:11.0.4'


// firebase 核心库
compile 'com.google.firebase:firebase-core:11.0.4'
compile 'com.google.firebase:firebase-messaging:11.0.4'                                   

两个服务如下:

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
    private static final String TAG = "MyFirebaseIIDService";

    @Override
    public void onCreate() {
        super.onCreate();
        FirebaseMessaging.getInstance().subscribeToTopic("CallBlackTimingDialog");
        FirebaseMessaging.getInstance().subscribeToTopic("CallBlackTimingDialog_2");
    }

    @Override
    public void onTokenRefresh() {
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.e("AAA", "Refreshedtoken:" + refreshedToken);
        sendRegistrationToServer(refreshedToken);
    }

    private void sendRegistrationToServer(String token) {
        // TODO: Implement this method to send token to your app server.
        Firebase.getInstance(this).logEvent("user_id", token);
    }

}

其中:

FirebaseMessaging.getInstance().subscribeToTopic("CallBlackTimingDialog");

这是关键字,作为识别你的应用的地方,在发云消息的命令中会用到这个关键字。

public class MyFirebaseMessagingService extends FirebaseMessagingService {


    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        System.out.println("MyFirebaseMessagingService.onMessageReceived------->"+remoteMessage.getFrom());
        //  加主题判断
        String from = remoteMessage.getFrom();
        //  弹出弹窗
        if (from != null && !from.equals("")) {
            if (from.equals("/topics/CallBlackTimingDialog")) {
                Map<String, String> data = remoteMessage.getData();
                if (data != null) {
                    String title = data.get("notificationTitle");
                    String message = data.get("notificationMessage");
                    String content = data.get("notificationContent");
                    String imageUrl = data.get("notificationUrl");

                    Intent intent = new Intent(MyFirebaseMessagingService.this,
                            DialThemeDialogActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("notificationUrl", imageUrl);
                    startActivity(intent);
                    Firebase.getInstance(this).logEvent("云弹窗", "接收到云弹窗");
                }
                String Token = FirebaseInstanceId.getInstance().getToken();
                Firebase.getInstance(this).logEvent("user_id", Token);
            } else if (from.equals("/topics/CallBlackTimingDialog_2")) {
                Map<String, String> data = remoteMessage.getData();

                if (data != null) {
                    String title = data.get("notificationTitle");
                    String message = data.get("notificationMessage");
                    String url = data.get("notificationUrl");
                    Log.e("onMessageReceived",title+"==========="+message+"================"+url);
                    Intent intent = new Intent(MyFirebaseMessagingService.this,
                            PushNewThemeActivity_2.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("notificationUrl", url);
                    startActivity(intent);
                }

                String Token = FirebaseInstanceId.getInstance().getToken();
                Firebase.getInstance(this).logEvent("user_id", Token);
            }
        }
    }
}

在上面这个服务中,会根据关键字进行判断,当你的app有多个云弹窗就需要进行判断收到的是哪个弹窗。进行判断后,弹出。

这就是全部的步骤。


下面面进行测试英道的工具PuTTY,这个工具我会进行上传,在这个工具中进行登录,并且输入命令,我对于这个还没有很好的理解,具体的原理不理解,只是会用,以后理解了里面的原理,为会在写下来的,有理解原理的麻烦留言给我讲讲。我的测试命令是:curl --header "Authorization: key=AAAAce1g-zA:APA91bG-viW-83nGsh_Ly5c7kCeH4gek5R_MCIG-LPWL3_xrdzvLfZIFFOqKqgSYEzDANGVux5C0BR2s5SiDz9jY9R2gh6GUYXmztwxpFwwNzaOMa4miKGKdZ0y3NvjgLhJkYwlKEJ_L" --header Content-Type:"application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"/topics/CallBlackTimingDialog\", \"priority\":\"high\", \"data\": {\"notificationTitle\":\"\",\"\":\"n\",\"notificationMessage\":\"\",\"notificationContent\":\"\",\"notificationUrl\":\"\"}}"

其中有个key:key=AAAAce1g-zA:APA91bG-viW-83nGsh_Ly5c7kCeH4gek5R_MCIG-LPWL3_xrdzvLfZIFFOqKqgSYEzDANGVux5C0BR2s5SiDz9jY9R2gh6GUYXmztwxpFwwNzaOMa4miKGKdZ0y3NvjgLhJkYwlKEJ_L, 这个如下图所示,获取的关键地方我都标注了:firebase 云通知功能使用。有独立的demo可用,还有测试工具。还有一个关键的地方:/topics/CallBlackTimingDialog\ ,这其中有一个关键字就是你的CallBlackTimingDialog写入server的。

这就是全部的 步骤。

资源在:https://download.csdn.net/download/qq_38998213/10478112。这里面包括项目 和测试工具,想要的私聊我就行,下载还得1积分,设的最低,感觉应该不设这个东西。