【问题标题】:How to do push notification from server to android mobile如何从服务器推送通知到安卓手机
【发布时间】:2012-06-24 11:57:47
【问题描述】:

我对推送通知一无所知。我在尝试学习。但我不明白。

我在服务器系统中有一个表 MySQL 数据库。当表格中发生任何更改时,我希望在 android 移动应用上显示通知。

谁能给点建议?

【问题讨论】:

  • 我登录了 c2dm 应用程序并填写了 singup,然后我收到了来自 google 的消息。在那之后我能做什么。我不知道请告诉我解决方案。我在过去 7 天里遇到了这个问题.. 请帮助我
  • 看看我在上面评论中给出的链接..
  • i sea ur 上面的评论。但我听不懂,请告诉我程序。我可以按照你的程序..请告诉我

标签: android push-notification


【解决方案1】:

实际上现在最近主要用于该 u 项目中的推送通知 FCM .... 构建推送通知的最佳链接:link

执行推送通知的步骤 - 适用于 Android 的 Firebase 云消息传递教程

  1. 转到 firebase 控制台并创建一个新项目。
  2. 现在输入您的应用名称并选择您所在的国家/地区。
  3. 现在点击将 Firebase 添加到您的 Android 应用。
  4. 现在您必须输入您的项目包名称并点击添加应用程序。
  5. 点击添加应用后,您将获得 google-services.json 文件。

在应用端

  1. 现在回到您的 android 项目。转到 app 文件夹并粘贴 google-services.json 文件
  2. 现在转到您的根级 build.gradle 文件并添加以下代码。

    一个。添加这一行 类路径 'com.google.gms:google-services:3.0.0'

    b.添加这一行 编译 'com.google.firebase:firebase-messaging:9.0.0'

  3. 现在同步您的项目。

  4. 创建一个名为 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 
        }
    }
    
  5. 现在创建 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());
        }
    }
    
  6. 现在我们必须在我们的 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>
    

终于

转到 firebase 控制台并选择您创建的应用。 从左侧菜单中选择通知。 点击新消息。 输入消息,选择单个设备并粘贴您复制的令牌,然后单击发送。和我在视频上做的一样,检查你的设备

【讨论】:

  • 谢谢!我从近 2 天开始一​​直在寻找这个。仍然不知道为什么谷歌允许火力基地处理通知?
  • 警告,这个答案已经 5 岁了。从FirebaseMEssagingService获取新的令牌覆盖onNewToken
【解决方案2】:

第一件事 - Google Push Notifications 称为 GCM(Google Cloud Messaging)。错误的名称使用可能会导致您获得错误的信息或教程。另一件事,您应该依赖开发人员。在这种情况下,从 Google Developers 网站开始,您可以在其中找到大部分基本信息和代码示例。 https://developers.google.com/cloud-messaging/.

更新

GCM 已弃用,您应该使用Firebase Cloud Messaging (FCM)

【讨论】:

    【解决方案3】:

    这里有一个很好的解释:
    http://quickblox.com/developers/SimpleSample-messages_users-android

    总体步骤是:

    1. 创建一个 google API 项目
    2. 为项目启用推送通知并获取 API 密钥
    3. 通过安卓应用获取注册ID(每台设备都有特定应用的注册ID)
    4. 创建一个服务器应用程序,通过 GSM 通过谷歌服务器将您的推送消息作为推送通知发送
    5. 在应用端收到推送通知时创建通知

    这不是我可以详细写在这里的东西。每一步都使用 Google。

    【讨论】:

      【解决方案4】:

      您可以查看 Firebase...查看此链接

      https://firebase.google.com/docs/cloud-messaging/

      此链接足以了解推送通知

      至于在数据库中的数据发生变化时发送通知,请让您的 API 向 FCM 服务器发送请求,以便将必要的数据传递给客户端。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-08
        • 1970-01-01
        • 2017-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多