Android 如何将通知从服务器推送到安卓手机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11151894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do push notification from server to android mobile
提问by naveen
I don't know anything about push notification. I am trying to learn. but I don't understand.
我对推送通知一无所知。我在尝试学习。但我不明白。
I have one table MySQL database in server system. When any changes are made in the table I want display notification on an android mobile app.
我在服务器系统中有一个表 MySQL 数据库。在表中进行任何更改时,我希望在 android 移动应用程序上显示通知。
Can anyone provide any suggestions?
任何人都可以提供任何建议吗?
回答by Amir Ziarati
here is a good explanation about this:
http://quickblox.com/developers/SimpleSample-messages_users-android
这里有一个很好的解释:http:
//quickblox.com/developers/SimpleSample-messages_users-android
The overall steps are:
总体步骤是:
- Create a google API project
- Enable push notifications for the project and get a API key
- Get a registration ID through android app (each device has a registration ID for a specific application)
- Create a server application to send your push messages as push notifications through google servers by GSM
- Create a notification when you get the push notification on the application side
- 创建一个谷歌 API 项目
- 为项目启用推送通知并获取 API 密钥
- 通过android app获取注册ID(每个设备都有一个特定应用的注册ID)
- 创建一个服务器应用程序,通过 GSM 通过谷歌服务器将您的推送消息作为推送通知发送
- 在应用端收到推送通知时创建通知
It's not something i can write all here by details. Use Google for every step.
这不是我可以在这里详细写的东西。每一步都使用谷歌。
回答by Sagar Chorage
actually now recently mostly use for push notification FCM inside that u project .... best link for build the push notication: link
实际上现在最近主要用于你项目中的推送通知 FCM .... 构建推送通知的最佳链接:链接
steps for perform push notification - Firebase Cloud Messaging Tutorial for Android
执行推送通知的步骤 - Firebase Cloud Messaging Tutorial for Android
- Go to firebase console and create a new project.
- Now put your app name and select your country.
- Now click on Add Firebase to Your Android App.
- Now you have to enter your projects package name and click on ADD APP.
- After clicking add app you will get google-services.json file.
- 转到 firebase 控制台并创建一个新项目。
- 现在输入您的应用名称并选择您所在的国家/地区。
- 现在点击将 Firebase 添加到您的 Android 应用程序。
- 现在您必须输入您的项目包名称并单击添加应用程序。
- 单击添加应用程序后,您将获得 google-services.json 文件。
On App side
在应用端
- Now come back to your android project. Go to app folder and paste google-services.json file
Now go to your root level build.gradle file and add the following code.
a. Add this line classpath 'com.google.gms:google-services:3.0.0'
b. Add this line compile 'com.google.firebase:firebase-messaging:9.0.0'
Now sync your project.
Create a class named MyFirebaseInstanceIDService.java and write the following code:
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { private static final String TAG = "MyFirebaseIIDService"; @Override public void onTokenRefresh() { //Getting registration token String refreshedToken = FirebaseInstanceId.getInstance().getToken(); //Displaying token on logcat Log.d(TAG, "Refreshed token: " + refreshedToken); } private void sendRegistrationToServer(String token) { //You can implement this method to store the token on your server //Not required for current project } }
Now create MyFirebaseMessagingService.java and write the following code:
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 android.util.Log; import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; /** * */ public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = "MyFirebaseMsgService"; @Override public void onMessageReceived(RemoteMessage remoteMessage) { //Displaying data in log //It is optional Log.d(TAG, "From: " + remoteMessage.getFrom()); Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); //Calling method to generate notification sendNotification(remoteMessage.getNotification().getBody()); } //This method is only generating push notification //It is same as we did in earlier posts 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("Firebase Push Notification") .setContentText(messageBody) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notificationBuilder.build()); } }
Now we have to define the above services in our AndroidManifest.xml file. So go to manifest and modify as follows.
<!-- Adding Internet Permission --> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- Defining Services --> <service android:name=".MyFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service> <service android:name=".MyFirebaseInstanceIDService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service> </application>
- 现在回到你的 android 项目。转到应用程序文件夹并粘贴 google-services.json 文件
现在转到您的根级别 build.gradle 文件并添加以下代码。
一种。添加这一行类路径 'com.google.gms:google-services:3.0.0'
湾 添加这一行 compile 'com.google.firebase:firebase-messaging:9.0.0'
现在同步您的项目。
创建一个名为 MyFirebaseInstanceIDService.java 的类并编写以下代码:
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { private static final String TAG = "MyFirebaseIIDService"; @Override public void onTokenRefresh() { //Getting registration token String refreshedToken = FirebaseInstanceId.getInstance().getToken(); //Displaying token on logcat Log.d(TAG, "Refreshed token: " + refreshedToken); } private void sendRegistrationToServer(String token) { //You can implement this method to store the token on your server //Not required for current project } }
现在创建 MyFirebaseMessagingService.java 并编写以下代码:
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 android.util.Log; import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; /** * */ public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = "MyFirebaseMsgService"; @Override public void onMessageReceived(RemoteMessage remoteMessage) { //Displaying data in log //It is optional Log.d(TAG, "From: " + remoteMessage.getFrom()); Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); //Calling method to generate notification sendNotification(remoteMessage.getNotification().getBody()); } //This method is only generating push notification //It is same as we did in earlier posts 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("Firebase Push Notification") .setContentText(messageBody) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notificationBuilder.build()); } }
现在我们必须在我们的 AndroidManifest.xml 文件中定义上述服务。所以去manifest修改如下。
<!-- Adding Internet Permission --> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- Defining Services --> <service android:name=".MyFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service> <service android:name=".MyFirebaseInstanceIDService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service> </application>
finally
最后
Go to firebase console and select the app you created. From the left menu select notification. Click on new message. Enter message, select single device and paste the token you copied and click on send. The same as I did on the video, and check your device
转到 firebase 控制台并选择您创建的应用程序。从左侧菜单中选择通知。单击新消息。输入消息,选择单个设备并粘贴您复制的令牌,然后单击发送。和我在视频上做的一样,检查你的设备
回答by haider_kazal
You can check out Firebase... Check out this link
您可以查看 Firebase ... 查看此链接
https://firebase.google.com/docs/cloud-messaging/
https://firebase.google.com/docs/cloud-messaging/
https://firebase.google.com/docs/notifications/
https://firebase.google.com/docs/notifications/
This links are sufficient to learn about Push Notification
此链接足以了解推送通知
And as for sending notification when data in database changes, make your API send a request to FCM server so that it delivers necessary data to clients.
至于在数据库中的数据发生变化时发送通知,让您的 API 向 FCM 服务器发送请求,以便它向客户端提供必要的数据。
回答by Tadas
The first thing - Google Push Notifications are called GCM (Google Cloud Messaging). Wrong name usage might lead you to wrong information or tutorial. The other thing, you should rely on Developer. In this case start from Google Developers website, where you will find most of basic info and code examples to start with. https://developers.google.com/cloud-messaging/.
第一件事 - 谷歌推送通知被称为 GCM(谷歌云消息)。错误的名称使用可能会导致您获得错误的信息或教程。另一件事,你应该依赖Developer。在这种情况下,从 Google Developers 网站开始,在那里您可以找到大多数基本信息和代码示例。https://developers.google.com/cloud-messaging/。
Update
更新
GCMis deprecated, you should use Firebase Cloud Messaging (FCM)
GCM已弃用,您应该使用Firebase Cloud Messaging (FCM)