您好我正在开发使用Firebase云消息传递的应用,我正在尝试在应用收到使用Firebase云消息传递的消息时显示通知。
应用正在运行时
现在当App关闭而不是在后台运行时(这些是需要解决的主要问题,需要解释这种不自然的行为)
以下是代码片段。
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"));
}
}
答案 0 :(得分:2)
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());
答案 1 :(得分:1)
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());
答案 2 :(得分:1)
试试这个:
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());
答案 3 :(得分:1)
Firebase通知
1.如果您通过firebase控制台发送通知,firebase将在此消息中添加通知对象
{
//..
"notification" : {
"body" : "message body sample text",
"title" : "notification Tittle",
"icon" : "icon information sample"
}
//..
}
1)如果您的通知包含此通知有效负载,并且如果您的应用位于后台,则
2)如果您的通知包含此通知有效负载 + 数据有效负载,并且如果您的应用位于后台,则
3)如果您的通知仅包含数据有效负载({data" : {"body" : "message body sample text","title" : "notification Tittle","icon" : "icon information sample"}
),并且如果您的应用位于后台或前景,那么,
因此,如果您希望每次收到通知时都执行OnMessageReceived()
方法,那么从服务器端开始,您应始终只传递数据有效负载。否则OnMessageReceived()
将不会已执行,Firebase将处理显示事件的通知。