前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >firebase 云通知功能使用。有独立的demo可用,还有测试工具。

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

作者头像
zhangjiqun
发布2024-12-16 12:51:46
发布2024-12-16 12:51:46
6800
代码可运行
举报
文章被收录于专栏:计算机工具
运行总次数:0
代码可运行

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

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

2.添加你的应用到Firebase,需要你的包名等,如下图:

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

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

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

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

首先你导包:

代码语言:javascript
代码运行次数:0
复制
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'                                   
代码语言:javascript
代码运行次数:0
复制

两个服务如下:

代码语言:javascript
代码运行次数:0
复制
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);
    }

}
代码语言:javascript
代码运行次数:0
复制

其中:

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

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

代码语言:javascript
代码运行次数:0
复制
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, 这个如下图所示,获取的关键地方我都标注了:

还有一个关键的地方:/topics/CallBlackTimingDialog\ ,这其中有一个关键字就是你的CallBlackTimingDialog写入server的。

这就是全部的 步骤。

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-06-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档