[英]firebase cloud messaging Android push notification not working
I have an android app that sends that is supposed to send a push notification to users. 我有一个发送的android应用程序,应该向用户发送推送通知。 When the notification is received by the user, the user taps on the notification and the app is opened and a url is opened in the android webview.
当用户收到通知时,用户点击通知并打开应用程序,并在android webview中打开URL。
but my app is not receiving any notification. 但我的应用程序未收到任何通知。 here is the code
这是代码
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));
}
}
The FirebaseMessagingService 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);
}
}
The Firebase Instance class Firebase实例类
public class MyFirebaseIdService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
String token = FirebaseInstanceId.getInstance().getToken();
sendToServer(token);
}
private void sendToServer(String token) {
}
}
Messages sent via the Firebase console are treated as notification
message payloads. 通过Firebase控制台发送的消息被视为
notification
消息有效负载。 From your code, you're only handling data
message payloads ( remoteMessage.getData()
which are probably null. You could include a data
message payload along with the notification
message contents by adding Advanced Option via the Firebase Console. 从代码中,您仅处理
data
消息有效负载( remoteMessage.getData()
可能为null。可以通过Firebase控制台添加“ 高级选项” ,将data
消息有效负载与notification
消息内容一起包括在内。
Also, FirebaseInstanceIdService
has been deprecated. 此外,
FirebaseInstanceIdService
已被弃用。 Proceed with using onNewToken()
in FirebaseMessagingService
. 在
FirebaseMessagingService
继续使用onNewToken()
。
First you are using LocalBroadcastManager
that's only registered if your MainActivity is currently in the foreground. 首先,您使用的是
LocalBroadcastManager
,它仅在MainActivity当前位于前台时才注册。
Secondly, what kind of message are you sending? 其次,您要发送什么样的消息? They could be data or notification messages.
它们可以是数据或通知消息。 If using data I would get rid of the LocalBroadcastManager and declare the showNotification method in MyFirebaseMessagingService so you can directly show the notification from there.
如果使用数据,我将摆脱LocalBroadcastManager并在MyFirebaseMessagingService中声明showNotification方法,以便您可以直接从那里显示通知。
If you are using notification messages. 如果您使用的是通知消息。 If your app is in the background the push notification would be handled by the System Tray, and the onMessageReceived would only be called if your App is in the foreground.
如果您的应用程序在后台,则推送通知将由系统托盘处理,并且仅当您的应用程序在前台时才调用onMessageReceived 。
Take a look at the docs here: https://firebase.google.com/docs/cloud-messaging/android/receive 在这里查看文档: https : //firebase.google.com/docs/cloud-messaging/android/receive
The most significant part: 最重要的部分:
onMessageReceived
is provided for most message types, with the following exceptions:大多数消息类型都提供
onMessageReceived
,但以下情况除外:
- Notification messages delivered when your app is in the background .
当您的应用程序在后台运行时发送通知消息 。 In this case, the notification is delivered to the device's system tray.
在这种情况下,通知将传递到设备的系统托盘。 A user tap on a notification opens the app launcher by default.
用户点击通知会默认打开应用启动器。
- Messages with both notification and data payload, both background and foreground .
同时具有通知和数据有效载荷(背景和前景)的消息 。 In this case, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
在这种情况下,通知将传递到设备的系统托盘,而数据有效载荷将在启动器活动的意图之外传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.