如何在应用程序关闭时向Android发送推送通知
我有一个小应用程序,当应用程序在后台运行时,我从Firabase cloud Messaging接收到消息。我对此进行了大量搜索,但在应用程序关闭时,我无法找到关于如何在android中接收/创建通知的正确答案,因此请不要认为这是一个重复的问题。有人能给我举个例子说明这一点以及它是如何做到的吗 这是我的Firebase消息服务类如何在应用程序关闭时向Android发送推送通知,android,firebase,firebase-cloud-messaging,Android,Firebase,Firebase Cloud Messaging,我有一个小应用程序,当应用程序在后台运行时,我从Firabase cloud Messaging接收到消息。我对此进行了大量搜索,但在应用程序关闭时,我无法找到关于如何在android中接收/创建通知的正确答案,因此请不要认为这是一个重复的问题。有人能给我举个例子说明这一点以及它是如何做到的吗 这是我的Firebase消息服务类 public class MyFirebaseMessagingService extends FirebaseMessagingService {
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static final String tag = "TAG";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(tag,"FROM"+remoteMessage.getFrom());
//check if message contains data
if(remoteMessage.getData().size()>0){
Log.d(tag,"Message Data" + remoteMessage.getData());
}
//check if message constains notification
if(remoteMessage.getNotification() != null){
sendNotification(remoteMessage.getNotification().getBody());
}
}
private void sendNotification(String body){
Intent i = new Intent(this,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Firebase CLoud Messaging")
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
}
}
这是我的清单
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
根据,您可以通过FCM发送两种类型的通知
切换到您自己的API,并发送数据消息。因此,几个小时后,在这个问题的帮助下,在@Mauker的帮助下,我终于做到了。这些是我采取的步骤和我从互联网上收到的所有信息 首先,忘记Firebase云消息,向您的移动应用程序发送通知。 第二,使用邮递员来做这些动作 通知有两种类型:所有人同时收到通知的组通知和通知本身仅供用户查看的直接通知 1º如果需要组通知,则必须在应用程序启动器类中执行以下操作:
FirebaseMessaging.getInstance().subscribeToTopic("groupNameChoosenByYou");
2º那么您必须创建一个类来处理此问题
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
String myCustomKey = data.get("title"); //received from postman POST as you can see above
Intent i = new Intent(this,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(myCustomKey)
.setContentText(myCustomKey+myCustomKey)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
}
}
在邮递员中4º,并在标题中执行此操作
Authorization -> Project settings in Firebase -> Cloud Messaging and take the Server key
Content-type -> application/json
5º如果您希望为中的某个特定用户执行直接通知
“to”:“/topics/groupNameChoosenByYou”
,替换为第一次与firebase连接时生成的设备令牌id(安装应用程序时)
6º如果你想在应用程序关闭时发送通知某些ROM不允许发送通知,除了facebook、whatsapp(黄金应用程序)等,你必须进行电池优化,并将你的应用程序放入受保护的应用程序(这会因品牌而异)。理想的方法是给用户一个初始弹出窗口来帮助他完成这项工作
这是我学到的,对我来说很有用。在上面的任何问题帖子中,我都会尝试对此进行更多研究,并在获得更多信息的同时进行更新。因此,经过几个小时的讨论,在@Mauker的帮助下,我终于解决了这个问题。这些是我采取的步骤和我从互联网上收到的所有信息 首先,忘记Firebase云消息,向您的移动应用程序发送通知。 第二,使用邮递员来做这些动作 通知有两种类型:所有人同时收到通知的组通知和通知本身仅供用户查看的直接通知 1º如果需要组通知,则必须在应用程序启动器类中执行以下操作:
FirebaseMessaging.getInstance().subscribeToTopic("groupNameChoosenByYou");
2º那么您必须创建一个类来处理此问题
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
String myCustomKey = data.get("title"); //received from postman POST as you can see above
Intent i = new Intent(this,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(myCustomKey)
.setContentText(myCustomKey+myCustomKey)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
}
}
在邮递员中4º,并在标题中执行此操作
Authorization -> Project settings in Firebase -> Cloud Messaging and take the Server key
Content-type -> application/json
5º如果您希望为中的某个特定用户执行直接通知
“to”:“/topics/groupNameChoosenByYou”
,替换为第一次与firebase连接时生成的设备令牌id(安装应用程序时)
6º如果你想在应用程序关闭时发送通知某些ROM不允许发送通知,除了facebook、whatsapp(黄金应用程序)等,你必须进行电池优化,并将你的应用程序放入受保护的应用程序(这会因品牌而异)。理想的方法是给用户一个初始弹出窗口来帮助他完成这项工作
这是我学到的,对我来说很有用。如果上面有任何问题,我将尝试对此进行更多研究,并在获得更多信息的同时进行更新。我的解决方法是将通知包含在我发送的消息中,因为如果只是FCM消息,而应用程序正在休眠,则消息会丢失,将消息优先级设置为“高”本应有效,但并非始终有效。如果您提供一个通过FCM发送的json示例,则会有所帮助。请稍候!!我想我看不到FCMAre中的JSON。您是否在控制台中手动发送消息?您确定该消息包含数据有效负载吗?如果没有数据有效负载,则不会触发MyFirebaseMessagingService。在console中,我想您必须填写“高级选项”部分。我的解决方法是包括对我发送的消息的通知,因为如果只是FCM消息,而应用程序正在休眠,消息就会丢失,将消息优先级设置为“高”本应有效,但并非始终有效。如果您提供一个通过FCM发送的json示例,则会有所帮助。请稍候!!我想我看不到FCMAre中的JSON。您是否在控制台中手动发送消息?您确定该消息包含数据有效负载吗?如果没有数据有效负载,则不会触发MyFirebaseMessagingService。在console中,我想您必须填写“高级选项”部分。那么,我需要在代码中做哪些更改?我正在编辑我的答案,以便更深入地了解这个问题。好的,但是