2017-10-09 8 views
0

私は2つのデバイスを持っています。 1つはAPI 26を実行しており、もう1つはAPI 21です。私はGoogleのfirebaseを使って通知しています。 API 26デバイスは、まずアプリを開くまでプッシュ通知を受け取りません。その後、それはすべてうまく受信します。 API 27デバイスのプッシュ通知を受け取るために、まずアプリケーションを開く必要はありません。アプリを開かなくてもいつもプッシュを受け取る方法についての考えはありますか?自分の電話をオンにするだけで通知がユーザーに届きたい。アプリがAndroidで閉じられているとプッシュ通知が表示されない

private void sendNotification(String title, String messageBody, String clickAction) { 

    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    String id = ""; 
    if (Build.VERSION.SDK_INT >= 26) { 
     id = "my_channel_01"; 
     // The user-visible name of the channel. 
     CharSequence name = "myChannel"; 
     // The user-visible description of the channel. 
     String description = ""; 
     int importance = NotificationManager.IMPORTANCE_HIGH; 
     NotificationChannel mChannel = new NotificationChannel(id, name,importance); 
     // Configure the notification channel. 
     mChannel.setDescription(description); 
     mChannel.enableLights(true); 
     // Sets the notification light color for notifications posted to this 
     // channel, if the device supports this feature. 
     mChannel.setLightColor(Color.RED); 
     mChannel.enableVibration(true); 
     mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); 
     notificationManager.createNotificationChannel(mChannel); 
    } 


    Intent intent = new Intent(this, MainActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

    String channelId = getString(R.string.default_notification_channel_id); 
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = 
      new NotificationCompat.Builder(this, id) 
        .setSmallIcon(R.mipmap.ic_launcher) 
        .setContentTitle(title) 
        .setContentText(messageBody) 
        .setAutoCancel(true) 
        .setSound(defaultSoundUri) 
        .setContentIntent(pendingIntent); 


    notificationManager.notify(1 /* ID of notification */, notificationBuilder.build()); 


} 
+0

firebaseからの通知をどのように送信しましたか?コンソールかAPIか? – zihadrizkyef

+0

api – RobOhRob

答えて

0

のJavaファイルに続いて作成し

<receiver 
      android:name=".OnBootBroadcastReceiver"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED"/> 
      </intent-filter> 
</receiver> 

マニフェストファイルに

をBootBroadcastReceiverを追加します。内部のFirebaseMessagingReceiveServiceを呼び出します。

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 

public class OnBootBroadcastReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent i = new Intent("com.demo.FirebaseMessagingReceiveService"); 
     i.setClass(context, FirebaseMessagingReceiveService.class); 
     context.startService(i); 
    } 
} 
+0

Hello Geniusを使用して投稿リクエストを使用して送信します。ありがとうございました!なぜこれがFireBaseメッセージングのチュートリアルに含まれていないのか分かりません。 – RobOhRob

関連する問題