从任务管理器

时间:2017-07-22 07:31:52

标签: android push-notification firebase-cloud-messaging

我制作的应用需要在应用关闭时收到通知。我正在使用fcm来获取通知。但是当应用程序从最近的任务中删除时,我会停止收到通知。我看过其他文章,但我仍然无法得到我的问题的答案。我正在使用firebase

  

'com.google.firebase:火力的消息:11.0.2'

请帮帮我。谢谢你提前。

MyFirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.e(TAG, "From: " + remoteMessage.getFrom());
    Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
    Log.e(TAG," remote :" +remoteMessage.toString() );
    try {
        JSONObject json = new JSONObject(remoteMessage.getData());
        String title = json.get("title").toString();
        String message = json.get("message").toString();
        createNottification(title, message);
    }
    catch (Exception ex){}

    if(remoteMessage.getNotification() != null)
        Log.d(TAG, "Message Notification Body: "+remoteMessage.getNotification().getBody());
}

private void createNottification(String title,String message)
{
    Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);;
    Notification notification = new NotificationCompat.Builder(this)
            .setContentTitle(title)
            .setContentText(message)
            .setSmallIcon(R.mipmap.pmi_launcher)
            .setSound(notificationSoundURI                )
            .build();

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());
    notificationManager.notify(Config.NOTIFICATION_ID, notification);

}
}

的Manifest.xml

 <!-- Firebase Notifications -->
    <service android:name=".services.MyFirebaseMessagingService" android:stopWithTask="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <service android:name=".services.MyFirebaseInstanceIDService"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>

1 个答案:

答案 0 :(得分:0)

首先传递PendingIntent并传递唯一的通知ID,以生成一堆或一堆通知。也设置Flags通知Intent.so就这样做了。希望它对您和其他用户有所帮助。

 private void sendNotification(String message,String title) {
    int id = (int) System.currentTimeMillis();
    Intent notificationIntent = new Intent(this, SplashActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(title)
            .setContentText(message)
            //.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image).setSummaryText(message))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
    } else {
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
    }

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(id, notificationBuilder.build());
}