【问题标题】:GCM Push Notification not received未收到 GCM 推送通知
【发布时间】:2016-11-08 10:08:29
【问题描述】:

我尝试this 代码用于 GCM 推送通知。 在此发送通知中,我使用this。但它会显示类似 Cool!消息发送成功检查您的设备...但我的设备没有收到通知。

【问题讨论】:

    标签: android


    【解决方案1】:

    试试 Firebase,这里是 Push-Notification 的文档https://firebase.google.com/docs/cloud-messaging/ 或者你可以查看这个教程https://www.simplifiedcoding.net/android-push-notification-tutorial-using-firebase/

    【讨论】:

      【解决方案2】:

      您应该使用 FCM。见

      1) 将此行添加到 build.gradle:
      dependencies { compile 'com.google.firebase:firebase-messaging:9.8.0'}

      2) 将此服务添加到清单中。
      <service android:name=".MyFirebaseInstanceIDService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service>
      3)将此类添加到您的项目中。我建议你保存你的 FCM Token:

      public class MyInstanceIDListenerService extends FirebaseInstanceIdService {
      
      
      @Override
      public void onTokenRefresh() {
          // Get updated InstanceID token.
          String refreshedToken = FirebaseInstanceId.getInstance().getToken();
          Log.d("FirebaseService", "Refreshed token: " + refreshedToken);
      
          SharedPreferences sharedPref = getSharedPreferences("YOUR_SETTING_NAME", Context.MODE_PRIVATE);
          //There are optional steps
          SharedPreferences.Editor editor = sharedPref.edit();
          editor.putString("token", refreshedToken);
          //notify the token update 
          editor.putBoolean("tokenUpdate",true);
          editor.commit();
          //You can send this token to your server (if you have)
          sendServer(refreshedToken)
          }
      }
      

      4) 现在将您的应用注册到https://console.firebase.google.com/。这将生成一个 JSON 文件,您必须将其放入应用文件夹中。

      5) 要生成 FCM 消息,您有 2 种可能性:
      5.1) 使用默认的 Firebase 控制台:https://console.firebase.google.com/project/fantamanager-2017/notification/compose
      5.2) 构建您的服务器应用程序(如果需要,我可以将我的 Php 服务器代码发送给您)

      6) 将此服务添加到您的 Manifest.xml

      <service android:name="FcmBroadcastReceiver">
                  <intent-filter>
                      <action android:name="com.google.firebase.MESSAGING_EVENT" />
                  </intent-filter>
      </service>
      

      并创建一个可以拦截推送的类:

       public class MyFirebaseMessagingService extends FirebaseMessagingService {
          private static final ID_NOTIFICATION = ...
          @Override
          public void onMessageReceived(RemoteMessage remoteMessage) {
      
             String from,data;
             from=remoteMessage.getFrom());
      
      
              // Check if message contains a notification payload.
              if (remoteMessage.getNotification() != null) {
                 data=remoteMessage.getNotification().getBody());
              }
      
              // Also if you intend on generating your own notifications as a result of a received FCM
              // message, here is where that should be initiated. See sendNotification method below.
              sendNotification(data);
          }
      
      
      
          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 /* Request code */, intent,
                      PendingIntent.FLAG_ONE_SHOT);
      
              Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
              NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                      .setSmallIcon(R.drawable.ic_stat_ic_notification)
                      .setContentTitle("FCM Message")
                      .setContentText(messageBody)
                      .setAutoCancel(true)
                      .setSound(defaultSoundUri)
                      .setContentIntent(pendingIntent);
      
              NotificationManager notificationManager =
                      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      
              notificationManager.notify(ID_NOTIFICATION, notificationBuilder.build());
          }
      }
      

      或者,如果您有自定义服务,请更改 onMessageReceived 函数的主体:

         Map data = message.getData();
          if (data.containsKey(YOUR_DATA_FIELD_1)) {
              String field1= data.get(YOUR_DATA_FIELD_1).toString();
              String field2= data.get(YOUR_DATA_FIELD_2).toString();
              ....
              sendNotification(field1,field2...);
              return;
          }
      

      【讨论】:

      • @PoonamDesai 它是通用编号...它允许您管理此类之外的通知
      【解决方案3】:

      您尝试了部分代码还是整个代码?因为我上次看到,gcm 的新应用程序已关闭。只有 FCM 可用。所以你需要像他们说的那样注册,在app中实现json文件并继续。如果您已经有一个实现 gcm 的项目并且您尝试了此代码的一部分,那么请检查密钥是否正确并且您没有从该项目或其他东西中复制它们的密钥。或者清单文件中的某些元数据可能丢失。

      【讨论】:

      • 我尝试了整个部分代码。我还将所有权限都放在清单中。虽然它不工作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多