如何在android中实现推送通知

如何在android中实现推送通知,android,push-notification,Android,Push Notification,我正在做很多关于推送通知的研究,但我不知道如何在安卓1.6中实现。我想问一下这方面的要求是什么?我们从服务器端以标签形式或仅以信息形式获得哪种类型的信息?与此相关的输入或输出是什么。我向服务器提供了哪些输入,哪些输出来自服务器。 这需要任何设备id吗?请告诉我谢谢 这是谷歌提供的文档。他们将PushNotification的概念命名为C2DM(云到设备的消息传递) 您可以通过访问给定的链接获得清晰的描述。我将简短地回答你的问题 你不能在安卓1.6中实现这一点。你需要2.2或更高 版本 作为Pu

我正在做很多关于推送通知的研究,但我不知道如何在安卓1.6中实现。我想问一下这方面的要求是什么?我们从服务器端以标签形式或仅以信息形式获得哪种类型的信息?与此相关的输入或输出是什么。我向服务器提供了哪些输入,哪些输出来自服务器。 这需要任何设备id吗?请告诉我谢谢

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

您可以通过访问给定的链接获得清晰的描述。我将简短地回答你的问题

  • 你不能在安卓1.6中实现这一点。你需要2.2或更高 版本
  • 作为PushNotification,我们只会收到警报,而不会收到完整的详细信息
  • 作为第三方服务器的输入,应具有C2DM的设备注册ID
  • 是的,应该有一个设备id来标识激活服务的设备。您可以在Android应用程序尝试连接C2DM的初始阶段获得它
这是谷歌提供的最新文档。他们将PushNotification的概念命名为C2DM(云到设备的消息传递)

您可以通过访问给定的链接获得清晰的描述。我将简短地回答你的问题

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

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

  • 默认情况下,您只需要打开应用程序(如启动屏幕)
  • 重定向到应用程序中的特定屏幕
  • 默认情况下,您只需要打开应用程序(如启动屏幕) 创建名为“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>
            ....
    

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

  • 默认情况下,您只需要打开应用程序(如启动屏幕)
  • 重定向到应用程序中的特定屏幕
  • 默认情况下,您只需要打开应用程序(如启动屏幕) 创建名为“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>
            ....
    

    不断地问同一个问题不会让你得到更好的答案:不断地问同一个问题不会让你得到更好的答案: