【问题标题】:firebase cloud messaging Android push notification not workingfirebase 云消息传递 Android 推送通知不起作用
【发布时间】:2018-09-07 04:01:25
【问题描述】:

我有一个发送应该向用户发送推送通知的 android 应用程序。当用户收到通知时,用户点击通知并打开应用程序并在android webview中打开一个url。

但我的应用没有收到任何通知。 这是代码

    public class MainActivity extends AppCompatActivity {
         private WebView webView;
         private ProgressDialog dialog;
         private BroadcastReceiver mRegistrationBroadcastReciever;
         private final String CHANNEL_ID="notificcation";        
         @Override
         protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_main);
             webView=(WebView)findViewById(R.id.webView);
             webView.getSettings().setJavaScriptEnabled(true);
             webView.setWebChromeClient(new WebChromeClient());
             webView.setWebViewClient(new WebViewClient(){
                  @Override
                  public void onPageFinished(WebView view, String url) {
                      if(dialog.isShowing())
                         dialog.dismiss();
                  }
             });     
             mRegistrationBroadcastReciever=new BroadcastReceiver() {
                  @Override
                  public void onReceive(Context context, Intent intent) {
                      if(intent.getAction().equals(Config.STR_PUSH)){
                          String message=intent.getStringExtra(Config.STR_MESSAGE);
                          showNotification("MSG",message);
                      }    
                  }
             };
             onNewIntent(getIntent());
         }
         @Override
         protected void onNewIntent(Intent intent) {
              dialog=new ProgressDialog(this);
              if(intent.getStringExtra(Config.STR_KEY)!=null){
                  dialog.show();
                  dialog.setMessage("Please Wait");
                  webView.loadUrl(intent.getStringExtra(Config.STR_KEY));    
              }
         }    
         private void showNotification(String title, String message) {
              Intent intent =new Intent(getBaseContext(),MainActivity.class);
              intent.putExtra(Config.STR_KEY,message);
              intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
              PendingIntent contentIntent=PendingIntent.getActivity(getBaseContext(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
              NotificationCompat.Builder builder=new NotificationCompat.Builder(getBaseContext(),CHANNEL_ID);
              builder.setAutoCancel(true)
              .setWhen(System.currentTimeMillis())
              .setDefaults(Notification.DEFAULT_ALL)
              .setSmallIcon(R.mipmap.ic_launcher_round)
              .setContentTitle(title)
              .setContentText(message)
              .setContentIntent(contentIntent);

              NotificationManager notificationManager = (NotificationManager)getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
              notificationManager.notify(1,builder.build());
         }
         @Override
         protected void onPause() {
                LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReciever);
              super.onPause();
         }
         @Override
         protected void onResume() {
              super.onResume();
              LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReciever,new IntentFilter("registration Complete"));
              LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReciever,new IntentFilter(Config.STR_PUSH));
         }
    }

FirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService{
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
       handleMessage(remoteMessage.getData().get(Config.STR_KEY));

    }

    private void handleMessage(String message) {
        Intent pushNotification=new Intent(Config.STR_PUSH);
        pushNotification.putExtra(Config.STR_MESSAGE,message);
        LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);


    }

}

Firebase 实例类

public class MyFirebaseIdService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();
        String token = FirebaseInstanceId.getInstance().getToken();
        sendToServer(token);

    }

    private void sendToServer(String token) {


    }
}

【问题讨论】:

  • 发送通知 Firebase 控制台的表单?
  • 从 Firebase 控制台

标签: android firebase push-notification firebase-cloud-messaging


【解决方案1】:

通过 Firebase 控制台发送的消息被视为 notification 消息负载。从您的代码中,您只处理 data 消息有效负载(remoteMessage.getData() 这可能为空。您可以通过添加 高级选项来包含 data 消息有效负载和 notification 消息内容> 通过 Firebase 控制台。

另外,FirebaseInstanceIdService 已被弃用。继续在FirebaseMessagingService 中使用onNewToken()

【讨论】:

    【解决方案2】:

    首先,您使用 LocalBroadcastManager,仅当您的 MainActivity 当前处于前台时才注册。

    其次,您要发送什么样的消息?它们可以是数据通知消息。如果使用 data 我会摆脱 LocalBroadcastManager 并在 MyFirebaseMessagingService 中声明 showNotification 方法,以便您可以直接从那里显示通知。

    如果您使用通知消息。如果您的应用在后台,推送通知将由系统托盘处理,并且只有当您的应用在前台时才会调用 onMessageReceived

    在此处查看文档:https://firebase.google.com/docs/cloud-messaging/android/receive

    最重要的部分:

    onMessageReceived 适用于大多数消息类型,但以下情况除外:

    • 当您的应用处于后台时发送的通知消息。在这种情况下,通知将传送到设备的系统托盘。默认情况下,用户点击通知会打开应用启动器。
    • 带有通知和数据负载的消息,包括后台和前台。在这种情况下,通知会传递到设备的系统托盘,而数据负载会在启动器 Activity 的 Intent 的附加部分中传递。

    【讨论】:

    • 在控制台中,我还发送了自定义数据,其中包括应用程序在点击通知后将打开的 url
    • 如果您同时发送通知和数据,则仅当您的应用程序处于前台(当前活动应用程序)时才会调用 onMessageReceived 否则系统托盘将处理它并根据您设置的内容显示通知在控制台中。
    • 即使应用程序处于前台,模拟器也不会收到通知。 genymotion
    • 您是否已将您的 Android 应用连接到 FirebaseApp?你有正确的.json 文件吗?试试我说的删除 LocalBroadcastManager。还要向 onMessageReceived 添加一些日志记录以检查它是否真的到达那里
    猜你喜欢
    • 2020-08-14
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    相关资源
    最近更新 更多