我正在开发一个通过Firebase发送通知的应用程序。我遵循了一些教程,其中的想法是在按下通知时打开一个活动。只要打开了该应用程序,此操作就可以正常运行,但是如果关闭或“最小化”该应用程序并按通知从默认活动(在我的情况下为SplashScreen
)中打开它,我真的不解释为什么它不起作用。有什么可以推荐的吗?
以下是我们称为MyFirebaseMessagingService
的类。如您所见,当我按下通知时,它会带我到NotificationActivity.class
,并且确实会…但是仅在打开应用程序的情况下。
package com.asecum.com.campussicapp;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by asecum5 on 24/08/17.
*/
public class Firebase_NotificationService extends FirebaseMessagingService {
private static final String TAG = "NOTICIAS";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
/*String from = remoteMessage.getFrom();
Log.d(TAG, "Mensaje recibido de: " + from);
if(remoteMessage.getNotification() != null) {
Log.d(TAG, "Notification: " + remoteMessage.getNotification().getBody());
showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
if(remoteMessage.getData().size()>0){
Log.d(TAG, "Data: " + remoteMessage.getData());
}*/
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Data: " + remoteMessage.getData());
try {
JSONObject data = new JSONObject(remoteMessage.getData());
String jsonMessage = data.getString("extra_information");
Log.d(TAG, "onMessageReceived: \n" + "Extra information: " + jsonMessage);
} catch (JSONException e) {
e.printStackTrace();
}
}
if (remoteMessage.getNotification() != null) {
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
showNotification(title, message);
}
}
private void showNotification(String title, String body) {
Intent intent = new Intent(this, NotificationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_home_black_24dp)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setVibrate(new long[]{100, 250, 100, 250, 100, 250})
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
接下来是清单...我可能会忘记一些许可吗?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.asecum.com.campussicapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".SplashScreen"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NotificationActivity">
<intent-filter>
<action android:name="NOTIFICATIONACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service
android:name=".Firebase_InstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name=".Firebase_NotificationService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
</application>
</manifest>
这就是我在
gradle
中添加的内容模块的build.gradle
dependencies {
...
compile 'com.google.firebase:firebase-core:10.2.6'
compile 'com.google.firebase:firebase-messaging:10.2.6'
testCompile 'junit:junit:4.12'
}
apply plugin: `com.google.gms.google-services`
项目的build.gradle
dependencies {
...
classpath 'com.google.gms:google-services:3.1.0'
}
最佳答案
我认为这是由the difference between "data" style cloud messages and "notification" style cloud messages in Firebase.引起的
对于通知样式消息:
FCM代表代表自动向最终用户设备显示消息
客户端应用程序。
另请注意,firebase控制台只能发送消息的通知样式。
可能是此通知样式云消息到达并由android系统而不是由您的应用程序自动处理(当应用程序关闭或在后台时)吗?
如果是这种情况,那么当用户单击通知时,系统将打开应用程序的默认活动(启动屏幕)。
或者,您可以send a "data" style cloud message将由您的应用而不是系统处理。然后,当用户单击通知时,它将执行.Firebase_NotificationService中定义的代码。
在Mac命令行中,您可以使用以下脚本通过Google的服务器将数据样式消息发送到特定的应用实例ID:
curl -X POST \
--Header "Authorization: key=<get authorization key from Firebase console - Project Settings - Web API Key>” \
--Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send \
-d " \
{ \
\"to\”:\”<get id for your particular android device app instance from Firebase Instance ID service in your app>\”, \
\"data\": \
{ \
\"title\":\" test title here \", \
\"body\":\" test body here \"
}, \
\"priority\": \"normal\" \
}"
请注意,JSON中没有“ notification”块。
另请参见this和this
关于java - 在Android Studio中关闭应用程序时,推送通知无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45944096/