如何解决Android后台通知无法打开应用
我正在从我的PHP调用FCM(不必担心PHP),并且它具有click_action =>'OPEN_APP',这意味着,至少在应用程序处于后台或已终止的情况下,通知应打开应用程序启动器活动,但实际上并没有。同时,如果应用程序处于前台状态,则通知有效负载可以正常工作,并且未决意图可以打开应打开的内容。
来自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>
来自MyFirebaseMessagingService.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);
}
}
我很好奇,是否可以使用时间限制作为通知ID,因为它们每个都需要唯一的ID?
int NOTIFICATION_ID = (int) System.currentTimeMillis();
来自OnBootbroadcastReceiver.class
的代码:
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);
}
}
来自Splashscreen.class
的代码:
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接收器,工作中的通知生成器。
我的启动器活动具有与PHP代码中提供的click_action相同的意图动作。
前景一切正常。
我什至在BOOT_COMPLETED
上开始了FCM接收服务,以便随时接收消息。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。