Android 通知意图未启动上述 Activity 和通知声音问题
Android Notification Intent Not launching the said Activity and Notification Sound Issue
您好,我正在开发一个使用 Firebase 云消息传递的应用程序,我正在尝试在应用程序使用 Firebase 云消息传递接收消息时显示通知。
当 App 为 运行
- 我可以听到通知声音
- 当我点击通知时,它会让我进入正确的 activity 在我的例子中 NotificationActivity.java
现在当 App 关闭时不在后台 运行(这些是需要解决的主要问题,需要解释这种不自然的行为)
- 我可以看到通知但听不到通知声音。
- 当我点击通知时它不会打开 NotificationActivity.java 但会打开 Splash.java 这是 default/launcher activity
下面是代码片段。
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"));
}
}
Intent resultIntent = null;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(context.getString(R.string.app_name))
.setAutoCancel(true)
.setContentText(message);
resultIntent = new Intent(context,Activity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(Activity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
5,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(pushId, mBuilder.build());
Use this code:
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
if (bitmaplogo != null) {
notificationBuilder.setLargeIcon(bitmaplogo);
} else {
notificationBuilder.setLargeIcon(largeIcon);
}
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setSmallIcon(R.drawable.app_icon_transparent);
} else {
notificationBuilder.setSmallIcon(R.mipmap.app_icon);
}
notificationBuilder.setColor(getResources().getColor(R.color.colorAccent));
notificationBuilder.setContentTitle(getResources().getString(R.string.app_name));
notificationBuilder.setContentText(messageBody);
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody));
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(defaultSoundUri);
notificationBuilder.setContentIntent(pendingIntent);
int num = (int) System.currentTimeMillis();
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(num, notificationBuilder.build());
试试这个:
Intent intent = new Intent(this, NotificationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle("Title")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
Firebase 通知
1.If 您正在通过 firebase 控制台发送通知,firebase 会像这样向您的消息添加一个通知对象
{
//..
"notification" : {
"body" : "message body sample text",
"title" : "notification Tittle",
"icon" : "icon information sample"
}
//..
}
1) 如果您的通知包含此 通知负载 并且如果您的应用程序处于 后台 那么,
- 通知发送到设备的系统托盘。用户点击通知会默认打开应用程序启动器。
2) 如果您的通知包含此 通知负载 + 数据负载 并且如果您的应用程序处于 后台 然后,
- 通知传送到设备的系统托盘,数据负载传送到启动器意图的附加部分 Activity。
3) 如果您的通知仅包含 数据负载 ({data" : {"body" : "message body sample text","title" : "notification Tittle","icon" : "icon information sample"}
) 并且如果您的应用程序处于 background 或 前景然后,
- 调用 Firebase 消息处理程序的 OnMessageReceived() 方法,我们可以编写代码以在此方法中显示通知。
所以如果你想每次收到通知时都执行OnMessageReceived()
方法,那么从服务器端你应该总是只传递数据有效负载。否则OnMessageReceived()
将不会被执行,Firebase 将处理显示内容的通知。
您好,我正在开发一个使用 Firebase 云消息传递的应用程序,我正在尝试在应用程序使用 Firebase 云消息传递接收消息时显示通知。
当 App 为 运行
- 我可以听到通知声音
- 当我点击通知时,它会让我进入正确的 activity 在我的例子中 NotificationActivity.java
现在当 App 关闭时不在后台 运行(这些是需要解决的主要问题,需要解释这种不自然的行为)
- 我可以看到通知但听不到通知声音。
- 当我点击通知时它不会打开 NotificationActivity.java 但会打开 Splash.java 这是 default/launcher activity
下面是代码片段。
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"));
}
}
Intent resultIntent = null;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(context.getString(R.string.app_name))
.setAutoCancel(true)
.setContentText(message);
resultIntent = new Intent(context,Activity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(Activity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
5,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(pushId, mBuilder.build());
Use this code:
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
if (bitmaplogo != null) {
notificationBuilder.setLargeIcon(bitmaplogo);
} else {
notificationBuilder.setLargeIcon(largeIcon);
}
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setSmallIcon(R.drawable.app_icon_transparent);
} else {
notificationBuilder.setSmallIcon(R.mipmap.app_icon);
}
notificationBuilder.setColor(getResources().getColor(R.color.colorAccent));
notificationBuilder.setContentTitle(getResources().getString(R.string.app_name));
notificationBuilder.setContentText(messageBody);
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody));
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(defaultSoundUri);
notificationBuilder.setContentIntent(pendingIntent);
int num = (int) System.currentTimeMillis();
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(num, notificationBuilder.build());
试试这个:
Intent intent = new Intent(this, NotificationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle("Title")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
Firebase 通知
1.If 您正在通过 firebase 控制台发送通知,firebase 会像这样向您的消息添加一个通知对象
{
//..
"notification" : {
"body" : "message body sample text",
"title" : "notification Tittle",
"icon" : "icon information sample"
}
//..
}
1) 如果您的通知包含此 通知负载 并且如果您的应用程序处于 后台 那么,
- 通知发送到设备的系统托盘。用户点击通知会默认打开应用程序启动器。
2) 如果您的通知包含此 通知负载 + 数据负载 并且如果您的应用程序处于 后台 然后,
- 通知传送到设备的系统托盘,数据负载传送到启动器意图的附加部分 Activity。
3) 如果您的通知仅包含 数据负载 ({data" : {"body" : "message body sample text","title" : "notification Tittle","icon" : "icon information sample"}
) 并且如果您的应用程序处于 background 或 前景然后,
- 调用 Firebase 消息处理程序的 OnMessageReceived() 方法,我们可以编写代码以在此方法中显示通知。
所以如果你想每次收到通知时都执行OnMessageReceived()
方法,那么从服务器端你应该总是只传递数据有效负载。否则OnMessageReceived()
将不会被执行,Firebase 将处理显示内容的通知。