如何在android中实现推送通知

发布于 2024-09-16 14:28:08 字数 151 浏览 5 评论 0原文

我正在对推送通知进行大量研究,但我不明白如何在 android 1.6 中实现。我想问一下这个有什么要求吗?我们从服务器端获取哪种类型的信息,无论是以标签形式还是仅以信息形式?与此相关的输入或输出是什么。我向服务器提供哪些输入以及来自服务器的输出。 是否需要任何设备 ID?请推荐我谢谢。

I am doing lots of Research on push notification but i don't understand how to implement in android 1.6. I want to ask what is the requirements for this? which type of information we get from the server end either in tags form or just information? what will be the input or output regarding this.Which input i give to the server and which output comes from the server.
is there any device id to be require for this? Please suggest me Thanks .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

歌入人心 2024-09-23 14:28:08

这是 Google 提供的文档的链接。他们将 PushNotification 的概念命名为 C2DM(云到设备消息传递)。

您可以通过访问给定的链接获得清晰的描述。我将为您的问题提供一些简短的回答。

  • 您无法在 Android 1.6 中实现此功能。您需要 2.2 或更高版本
    版本
  • 作为 PushNotification,我们只收到警报,而不是完整的详细信息。
  • 作为第三方服务器的输入,应具有 C2DM 的设备注册 ID。
  • 是的,应该有一个设备ID来识别设备来激活服务。您可以在 Android 应用程序尝试与 C2DM 连接的初始阶段获取它

This is the link to documentation given by the Google. They named the concept of PushNotification as C2DM (Cloud To Device Messaging)

You can get a clear description by visiting the given link. I'll give you some short answere for your questions.

  • You can't implement this in Android 1.6. You need 2.2 or higher
    version
  • As PushNotification, we get only alerts, not the full details message.
  • As input for the third party server, should have the device registration ID with the C2DM.
  • Yes, there should be a device id to identify the device to activate the service. You can get it at the initial phase where your Android app try to connect with the C2DM
红焚 2024-09-23 14:28:08

在Firebase中,我们可以将包含多条信息的通知推送到特定或多个设备,因为我们需要从android端实现一些代码,首先,我们需要在您的应用程序中设置firebase配置,我将介绍如何重定向将通知推送到移动应用程序中的特定或默认屏幕。
两种打开应用程序屏幕的方式

  1. 默认情况下,当您只需要打开应用程序时(如启动屏幕)。
  2. 重定向到应用程序中的特定屏幕。

默认情况下,当您只需要打开应用程序时(如启动屏幕)
创建一个名为“FirebaseMessagingService”的类并扩展“FirebaseMessagingService”

代码来实现

        public class FirebaseMessagingService extends FirebaseMessagingService
        {
            @Override
            public void onNewToken(String token)
            {
                sendRegistrationToServer(token);
            }

            public void onMessageReceived(RemoteMessage remoteMessage)
            {
                String title = remoteMessage.getNotification().getTitle();
                String body = remoteMessage.getNotification().getBody();
                Uri imageUrl = remoteMessage.getNotification().getImageUrl();
                String actionItem = remoteMessage.getNotification().getClickAction();

                if (imageUrl == null)
                {
                    MyNotificationManager.getmInstance(getApplicationContext()).displayNotificationAction(title, body,actionItem);
                } 
                else
                {
                    MyNotificationManager.getmInstance(getApplicationContext()).displayImageNotification(title, body, imageUrl);
                }
            }

            private void sendRegistrationToServer(String token)
            {
                // TODO: Implement this method to send a token to your app server.
            }

        }

创建通知管理器类来管理具有不同参数的显示方法

        public class MyNotificationManager
        {
            private Context mCtx;
            private static MyNotificationManager mInstance;

            private MyNotificationManager(Context context)
            {
                createNotificationChannel();
                mCtx = context;
            }

            public static synchronized MyNotificationManager getmInstance(Context context)
            {

                if (mInstance == null)
                {
                    mInstance = new MyNotificationManager(context);
                }
                return mInstance;
            }

            public void createNotificationChannel()
            {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
                {
                    int importance = NotificationManager.IMPORTANCE_DEFAULT;
                    NotificationChannel channel = new NotificationChannel("1", "Testing the Notification", importance);
                    channel.setDescription("We are testing the notification");
                }
            }

            public void displayNotification(String title, String body)
            {
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx, Constant.CHANNEL_ID)
                        .setSmallIcon(R.mipmap.ic_notification)
                        .setColor(ContextCompat.getColor(mCtx, R.color.colorPrimary))
                        .setContentTitle(title)
                        .setContentText(body);

                Intent intent = new Intent(mCtx, SplashActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
               
                mBuilder.setContentIntent(pendingIntent);
                NotificationManager mNotificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);


                if (mNotificationManager != null)
                {
                    mNotificationManager.notify(1, mBuilder.build());
                }

            }

            
            public void displayImageNotification(String title, String body, Uri imageUrl)
            {

                NotificationCompat.Builder notification = null;
                NotificationManager mNotificationManager = null;
                try
                {
                    notification = new NotificationCompat.Builder(mCtx, Constant.CHANNEL_ID)
                            .setSmallIcon(R.mipmap.ic_notification)
                            .setContentTitle(title)
                            .setAutoCancel(true)
                            .setColor(ContextCompat.getColor(mCtx, R.color.colorPrimary))
                            .setLargeIcon(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? Picasso.with(mCtx).load(imageUrl).get() : Picasso.with(mCtx).load(R.mipmap.ic_notification).get())
                            .setContentText(body)
                            .setStyle(new NotificationCompat.BigPictureStyle()
                                    .bigPicture(Picasso.with(mCtx).load(imageUrl).get())
                                    .bigLargeIcon(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? Picasso.with(mCtx).load(imageUrl).get() : Picasso.with(mCtx).load(R.mipmap.ic_notification).get()));

                   
                    Intent intent = new Intent(mCtx, SplashActivity.class);
                    PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

                    notification.setContentIntent(pendingIntent);
                    mNotificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);


                    if (mNotificationManager != null)
                    {
                        notification.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
                        mNotificationManager.notify(1, notification.build());
                    }
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        }

现在只需通过 Firebase 控制台触发通知或通过 API 发送,例如:-

        {
           "to": "device_token",
            "priority": "high",
           "notification": {
            "body": "Happy Coding",
            "title": "All things are difficult before they are easy.",
            "image":""
           },
           "data": {
            "image":""
           }
        }

2.重定向到中的特定屏幕该应用程序。
打开 AndroidManifest.xml 并在您需要定义的活动标记中...

        ....
                <activity
                    android:name=".activity.SpedificActivity"
                    android:screenOrientation="portrait"
                    android:theme="@style/AppTheme.NoActionBar" >
                    <intent-filter>
                        <action android:name="SpedificActivityNotification" />
                        <category android:name="android.intent.category.DEFAULT" />
                    </intent-filter>

                </activity>
        ....

现在调用 API

        {
           "to": "device_token",
            "priority": "high",
           "notification": {
            "body": "Happy Coding",
            "title": "All things are difficult before they are easy.",
            "click_action": "SpedificActivityNotification",
            "image":""
           },
           "data": {
            "image":""
           }
        }

In Firebase we can push notification with multiple pieces of information to the specific or multiple devices for that we need to implement some code from the android side, first, we need to set up the firebase configuration in your app, I will cover how to redirect push notification to specific or default a screen in the mobile application.
Two ways to open the application screen

  1. By default when you only need to open the application(Like splash screen).
  2. Redirect to the specific screen in the application.

By default when you only need to open the application(Like splash screen)
Create a class Named "FirebaseMessagingService" and extends "FirebaseMessagingService"

Code to implement

        public class FirebaseMessagingService extends FirebaseMessagingService
        {
            @Override
            public void onNewToken(String token)
            {
                sendRegistrationToServer(token);
            }

            public void onMessageReceived(RemoteMessage remoteMessage)
            {
                String title = remoteMessage.getNotification().getTitle();
                String body = remoteMessage.getNotification().getBody();
                Uri imageUrl = remoteMessage.getNotification().getImageUrl();
                String actionItem = remoteMessage.getNotification().getClickAction();

                if (imageUrl == null)
                {
                    MyNotificationManager.getmInstance(getApplicationContext()).displayNotificationAction(title, body,actionItem);
                } 
                else
                {
                    MyNotificationManager.getmInstance(getApplicationContext()).displayImageNotification(title, body, imageUrl);
                }
            }

            private void sendRegistrationToServer(String token)
            {
                // TODO: Implement this method to send a token to your app server.
            }

        }

Create Notification Manager class to manage the display method with different parameters

        public class MyNotificationManager
        {
            private Context mCtx;
            private static MyNotificationManager mInstance;

            private MyNotificationManager(Context context)
            {
                createNotificationChannel();
                mCtx = context;
            }

            public static synchronized MyNotificationManager getmInstance(Context context)
            {

                if (mInstance == null)
                {
                    mInstance = new MyNotificationManager(context);
                }
                return mInstance;
            }

            public void createNotificationChannel()
            {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
                {
                    int importance = NotificationManager.IMPORTANCE_DEFAULT;
                    NotificationChannel channel = new NotificationChannel("1", "Testing the Notification", importance);
                    channel.setDescription("We are testing the notification");
                }
            }

            public void displayNotification(String title, String body)
            {
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx, Constant.CHANNEL_ID)
                        .setSmallIcon(R.mipmap.ic_notification)
                        .setColor(ContextCompat.getColor(mCtx, R.color.colorPrimary))
                        .setContentTitle(title)
                        .setContentText(body);

                Intent intent = new Intent(mCtx, SplashActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
               
                mBuilder.setContentIntent(pendingIntent);
                NotificationManager mNotificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);


                if (mNotificationManager != null)
                {
                    mNotificationManager.notify(1, mBuilder.build());
                }

            }

            
            public void displayImageNotification(String title, String body, Uri imageUrl)
            {

                NotificationCompat.Builder notification = null;
                NotificationManager mNotificationManager = null;
                try
                {
                    notification = new NotificationCompat.Builder(mCtx, Constant.CHANNEL_ID)
                            .setSmallIcon(R.mipmap.ic_notification)
                            .setContentTitle(title)
                            .setAutoCancel(true)
                            .setColor(ContextCompat.getColor(mCtx, R.color.colorPrimary))
                            .setLargeIcon(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? Picasso.with(mCtx).load(imageUrl).get() : Picasso.with(mCtx).load(R.mipmap.ic_notification).get())
                            .setContentText(body)
                            .setStyle(new NotificationCompat.BigPictureStyle()
                                    .bigPicture(Picasso.with(mCtx).load(imageUrl).get())
                                    .bigLargeIcon(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? Picasso.with(mCtx).load(imageUrl).get() : Picasso.with(mCtx).load(R.mipmap.ic_notification).get()));

                   
                    Intent intent = new Intent(mCtx, SplashActivity.class);
                    PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

                    notification.setContentIntent(pendingIntent);
                    mNotificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);


                    if (mNotificationManager != null)
                    {
                        notification.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
                        mNotificationManager.notify(1, notification.build());
                    }
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        }

Now just trigger the notification through Firebase console or send through API like:-

        {
           "to": "device_token",
            "priority": "high",
           "notification": {
            "body": "Happy Coding",
            "title": "All things are difficult before they are easy.",
            "image":""
           },
           "data": {
            "image":""
           }
        }

2.Redirect to the specific screen in the application.
Open the AndroidManifest.xml and in activity tag you need to define ...

        ....
                <activity
                    android:name=".activity.SpedificActivity"
                    android:screenOrientation="portrait"
                    android:theme="@style/AppTheme.NoActionBar" >
                    <intent-filter>
                        <action android:name="SpedificActivityNotification" />
                        <category android:name="android.intent.category.DEFAULT" />
                    </intent-filter>

                </activity>
        ....

now call the API

        {
           "to": "device_token",
            "priority": "high",
           "notification": {
            "body": "Happy Coding",
            "title": "All things are difficult before they are easy.",
            "click_action": "SpedificActivityNotification",
            "image":""
           },
           "data": {
            "image":""
           }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文