【发布时间】:2017-05-23 11:51:50
【问题描述】:
您好,我正在开发一个使用 Firebase Cloud Messaging 的应用程序,并且我正在尝试在应用程序使用 Firebase Cloud Messaging 收到消息时显示通知。
应用运行时
- 我能听到通知声音
- 当我点击通知时,它会在我的情况下让我进入正确的活动 NotificationActivity.java
现在当应用关闭时不在后台运行(这些是需要解决的主要问题,需要对这种不自然的行为进行解释)
- 我可以看到通知但听不到通知声音。
- 当我点击通知时,它不会打开 NotificationActivity.java,而是打开 Splash.java 这是默认/启动器活动
以下是代码片段。
Mainfest.xml
<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=".Splash"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:label="@string/title_activity_login"
android:windowSoftInputMode="stateHidden" />
<activity
android:name=".SignupActivity"
android:windowSoftInputMode="stateHidden" />
<activity
android:name=".Dashboard"
android:label="@string/title_activity_dashboard" />
<activity android:name="pk.edu.kiu.jobslocatoer.NotificationActivity"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true">
<intent-filter>
<action android:name="pk.edu.kiu.jobslocatoer.MESSAGING_ACTIVTY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<receiver android:name=".receivers.NotificationReceiver">
<intent-filter>
<action android:name="pk.edu.kiu.jobslocatoer.MESSAGING_EVENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity android:name=".SettingsActivity"></activity>
<service android:name=".services.MessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".services.InstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
</application>
NotificationReceiver.java
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent notify = new Intent(context, NotificationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) SystemClock.currentThreadTimeMillis(), notify, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Titile")
.setContentText("Text")
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify((int) SystemClock.currentThreadTimeMillis(), notification);
}
}
MessagingService.java
public class MessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(GlobalConfig.TAG, GlobalConfig.getRef(this) + " notificaiton: " + remoteMessage.getNotification().getBody());
sendBroadcast(new Intent("pk.edu.kiu.jobslocatoer.MESSAGING_EVENT"));
}
}
【问题讨论】:
标签: android android-intent notifications android-pendingintent