我是Stackoverflow的新手,目前正在开发一个处理来自Fire Base的传入通知并打开我的应用程序的应用程序。当我在寻找解决方案时。目标是即使应用程序在后台且屏幕关闭(电话锁定),也可以接收通知。甚至我的应用程序被杀了,但我仍然希望我的通知能被类似whatsapp的应用程序接收。在whatsapp中,即使手机被锁定或应用被杀死,所有通知都收到,我想做同样的事情,但是我是android开发的新手,所以我不明白该怎么做。
当我的应用程序处于前台时,接收方会识别所有通知。 即使当应用程序在后台但我的手机仍在开机时,我也可以收到这些消息。 奇怪的事情发生在这里:
应用程序处于前台状态,我关闭了屏幕->可以识别通知。 应用程序在后台,我关闭了屏幕->通知将无法识别。
最大的奇怪之处是我在旧的micromax Unite 3手机中实现了目标。在此手机中,我什至收到了通知,甚至是我的背景或被杀死,但在我的redmi note 3中,当应用终止了通知,因此无法识别。
我想要解决这个问题。我希望我的通知能够被识别,甚至在所有版本的操作系统和手机中,应用程序都是前台,后台或被杀死。
我在下面使用firebase服务代码的简单onMessageReceived()方法
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if(remoteMessage.getData()!=null)
sendNotification(remoteMessage);
}
private void sendNotification(RemoteMessage remoteMessage) {
Map<String,String> data=remoteMessage.getData();
String title=data.get("title");
String content=data.get("content");
NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID="Gov_Job";
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
{
//Only active for Android o and higher because it need Notification Channel
@SuppressLint("WrongConstant") NotificationChannel notificationChannel=new NotificationChannel(NOTIFICATION_CHANNEL_ID,
"GovJob Notification",
NotificationManager.IMPORTANCE_MAX);
notificationChannel.setDescription("GovJob channel for app test FCM");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0,1000,500,1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
//.setSmallIcon(android.support.v4.R.drawable.notification_icon_background)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("Hearty365")
.setContentTitle(title)
.setContentText(content)
.setContentInfo("info");
notificationManager.notify(1,notificationBuilder.build());
}
答案 0 :(得分:0)
为了获得此功能,我使用BroadcastReceiver和Android Notification Service`
package com.alarmmanager_demo;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import static android.support.v4.content.WakefulBroadcastReceiver.startWakefulService;
/**
* Created by sonu on 09/04/17.
*/
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "ALARM!! ALARM!!", Toast.LENGTH_SHORT).show();
//Stop sound service to play sound for alarm
context.startService(new Intent(context, AlarmSoundService.class));
//This will send a notification message and show notification in notification tray
ComponentName comp = new ComponentName(context.getPackageName(),
AlarmNotificationService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
}
}
`
答案 1 :(得分:0)
在firebase中,这称为Firebase Cloud Messaging。在这种情况下,首先我将我的应用程序与Firebase连接。然后我在build.gradle(module-app)中实现了firebase-messaging
implementation 'com.google.firebase:firebase-messaging:11.8.0'
。
然后,我创建扩展FirebaseInstanceIdService的此类。因为Firebase为单个应用程序提供了单独的ID。
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
字符串REG_TOKEN =“ REG_TOKEN”;
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(REG_TOKEN,"Token " +refreshedToken);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
}
}
然后,我创建另一个扩展FirebaseMessagingService的类。 公共类MyFirebaseMessagingService扩展了FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Intent intent=new Intent(this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationbuilder= new NotificationCompat.Builder(this);
notificationbuilder.setContentTitle("FOR NOTIFICATION");
notificationbuilder.setContentText(remoteMessage.getNotification().getBody());
notificationbuilder.setAutoCancel(true);
notificationbuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationbuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationbuilder.build());
}
}
在此处输入代码
毕竟,我从firbase控制台发送了一条消息。这是我的手机收到的通知。