我正在创建一个使用Firebase通知的应用程序,除了用户点击通知,我想打开一个活动以及当用户点击后退按钮时,我希望返回主页活动,但现在应用仅在用户点击后退按钮时关闭。
这是我的清单代码:
@Autowired
private VelocityEngine velocityEngine;
public String prepareRegistrationEmailText(User user) {
VelocityContext context = new VelocityContext();
context.put("username", user.getUsername());
context.put("email", user.getEmail());
StringWriter stringWriter = new StringWriter();
velocityEngine.mergeTemplate("registration.vm", "UTF-8", context, stringWriter);
String text = stringWriter.toString();
return text;
}
和我发送通知的php脚本:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.massa.luxvilla">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name=".LuxVilla">
<meta-data
android:name="android.app.default_searchable"
android:value="com.example.massa.luxvilla.searchableactivity" />
<provider
android:name=".sugestoes.SearchSugestionsProvider"
android:authorities="com.example.massa.luxvilla.sugestoes.SearchSugestionsProvider" />
<activity android:name=".MainActivity"
android:theme="@style/Main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".searchableactivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity
android:name=".Actividades.casaactivity"
android:parentActivityName="com.example.massa.luxvilla.MainActivity"
android:theme="@style/Infocasa">
<intent-filter>
<action android:name="casaactivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.massa.luxvilla.MainActivity"/>
</activity>
<activity android:name=".Actividades.SettingsActivity"></activity>
<service
android:name=".notificacoes.FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".notificacoes.FirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
</application>
</manifest>
感谢。
答案 0 :(得分:0)
您可以在通过通知打开的Activity
中尝试这样的事情:
@Override
public void onBackPressed() {
if (isTaskRoot()) {
finish();
startActivity(new Intent(this, MainActivity.class));
} else {
super.onBackPressed();
}
}
如果任何Activity
在同一任务的堆栈中,您将检查,如果没有,您将打开MainActivity