Java Android后台通知无法打开应用程序
我从我的PHP调用FCM(不要担心PHP),它有click_action=>“OPEN_APP”,这意味着通知应该打开APP启动器活动,至少在APP处于后台或被杀死时是这样,但它没有。同时,若应用程序位于前台,通知有效负载工作正常,PendingEvent将打开它应该打开的内容 来自Java Android后台通知无法打开应用程序,java,android,firebase,firebase-cloud-messaging,Java,Android,Firebase,Firebase Cloud Messaging,我从我的PHP调用FCM(不要担心PHP),它有click_action=>“OPEN_APP”,这意味着通知应该打开APP启动器活动,至少在APP处于后台或被杀死时是这样,但它没有。同时,若应用程序位于前台,通知有效负载工作正常,PendingEvent将打开它应该打开的内容 来自manifest.xml的代码: <activity android:name=".SplashScreen" android:configChanges=&
manifest.xml
的代码:
<activity
android:name=".SplashScreen"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="OPEN_APP" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//other activities
<service
android:name=".services.MyFirebaseMessagingService"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/via" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorPrimaryDark" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/newsfeed_channel_id" />
</service>
<service
android:name=".receivers.OnBootBroadcastReceiver"
android:exported="true"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</service>
我好奇地问,既然每个时间工厂都需要一个唯一的id,那么使用时间工厂作为通知id可以吗
int NOTIFICATION_ID = (int) System.currentTimeMillis();
来自OnBootBroadcastReceiver.class的代码
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
//get key/value from notification
String lectureDate = remoteMessage.getData().get("lecture_date");
String web_url = remoteMessage.getData().get("web_url");
if(lectureDate != null)sendLecturesNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), lectureDate);
else Log.e(TAG, "Message.notification is empty!");
}
}
@Override
public void onNewToken(@NonNull String token) {
Log.d(TAG, "Refreshed token: " + token);
}
private void sendLecturesNotification(String title,String messageBody, String date) {
int NOTIFICATION_ID = (int) System.currentTimeMillis();
Intent intent;
intent = new Intent(this, Lectures_graph.class).putExtra("lecture_date", date)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, intent,
0);
String channelId = getString(R.string.lectures_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.via)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setCategory(NotificationCompat.CATEGORY_REMINDER)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}
Log.d(TAG, "Lectures notification built with date:"+date);
}
}
public class OnBootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent("com.demo.FirebaseMessagingReceiveService");
i.setClass(context, MyFirebaseMessagingService.class);
context.startService(i);
}
}
public class SplashScreen extends AppCompatActivity {
//length of splashscreen
private static int SPLASHSCREEN_LENGTH = 1250;
private String TAG = "MenuScreen";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
findViewById(R.id.welcome_screen).startAnimation(AnimationUtils.loadAnimation(this, R.anim.appear));
Handler handler = new Handler();
//closes splashscreen after given time
handler.postDelayed(new Runnable() {
public void run() {
//close welcome screen after given time
Bundle extras = getIntent().getExtras();
if(extras != null) {
//im handling notification extras here
}
finish();
}
}, SPLASHSCREEN_LENGTH);
}
}
来自Splashscreen.class的代码
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
//get key/value from notification
String lectureDate = remoteMessage.getData().get("lecture_date");
String web_url = remoteMessage.getData().get("web_url");
if(lectureDate != null)sendLecturesNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), lectureDate);
else Log.e(TAG, "Message.notification is empty!");
}
}
@Override
public void onNewToken(@NonNull String token) {
Log.d(TAG, "Refreshed token: " + token);
}
private void sendLecturesNotification(String title,String messageBody, String date) {
int NOTIFICATION_ID = (int) System.currentTimeMillis();
Intent intent;
intent = new Intent(this, Lectures_graph.class).putExtra("lecture_date", date)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, intent,
0);
String channelId = getString(R.string.lectures_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.via)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setCategory(NotificationCompat.CATEGORY_REMINDER)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}
Log.d(TAG, "Lectures notification built with date:"+date);
}
}
public class OnBootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent("com.demo.FirebaseMessagingReceiveService");
i.setClass(context, MyFirebaseMessagingService.class);
context.startService(i);
}
}
public class SplashScreen extends AppCompatActivity {
//length of splashscreen
private static int SPLASHSCREEN_LENGTH = 1250;
private String TAG = "MenuScreen";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
findViewById(R.id.welcome_screen).startAnimation(AnimationUtils.loadAnimation(this, R.anim.appear));
Handler handler = new Handler();
//closes splashscreen after given time
handler.postDelayed(new Runnable() {
public void run() {
//close welcome screen after given time
Bundle extras = getIntent().getExtras();
if(extras != null) {
//im handling notification extras here
}
finish();
}
}, SPLASHSCREEN_LENGTH);
}
}
那么我可能在哪里犯了错误
我有正在工作的FCM接收器,正在工作的通知生成器。
我的启动程序活动的intent操作与PHP代码中提供的click_操作相同。
前景中一切正常。
我甚至在
BOOT\u COMPLETED
上启动了FCM receiving服务,以便随时接收消息。我认为您在php请求中使用了通知负载。您应该使用数据负载,它将解决您的问题。