2017-11-23 3 views
-1

こんにちは、BroadcastReceiverを使用してプッシュ通知を表示しようとしていますが、アプリが最小化されているときにうまく動作しますが、プッシュ通知を表示できません。Android - アプリが強制終了されたときにもpushNotificationを表示するにはどうすればいいですか?

私の質問は、アプリが閉じられたときにプッシュ通知を表示する方法や解決策は何ですか?

怒鳴るは、放送受信機

public class Alarm extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     String message=intent.getStringExtra("message"); 
     String title=intent.getStringExtra("title"); 
     String click_action=intent.getStringExtra("click_action"); 
     notification(context,message,title,click_action,intent); 
    } 

    private void notification(Context context, String message, String title, String click_action, Intent intent) { 

     Toast.makeText(context, title + message + click_action, Toast.LENGTH_SHORT).show(); 
     if (click_action.equals("Time_LineActivity")) { 
      intent = new Intent(context, Time_LineActivity.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     } else if (click_action.equals("MainActivity")) { 
      intent = new Intent(context, MainActivity.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     } else { 
      intent = new Intent(context, MainActivity.class); 
      intent.addFlags(Intent.FLAG_FROM_BACKGROUND); 
     } 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     Notification.Builder notification = new Notification.Builder(context) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle(title) 
       .setContentText(message) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

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

     notificationManager.notify(0, notification.build()); 

    } 
} 

と怒鳴るを使用して通知するためのコードで私のFirebaseMessagingServiceコードで事前に

public class MyFirebaseMessagingService extends FirebaseMessagingService { 
    private static final String TAG = "MyFirebaseMsgService"; 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     Log.d(TAG, "From: " + remoteMessage.getFrom()); 
     Log.d(TAG, "Notification Message Body: " + remoteMessage.getData().get("title" +" "+"body" +" "+"click_action")); 
     String title=remoteMessage.getData().get("title"); 
     String message=remoteMessage.getData().get("body"); 
     String click_action=remoteMessage.getData().get("click_action"); 
     sendNotification(title,message,click_action); 
    } 
    private void sendNotification(String title, String message, String click_action) { 
     Intent broadcastedIntent=new Intent(this, Alarm.class); 
     broadcastedIntent.putExtra("message", message); 
     broadcastedIntent.putExtra("title", title); 
     broadcastedIntent.putExtra("click_action", click_action); 
     sendBroadcast(broadcastedIntent); 
    } 
} 

おかげで...すべてのあなたのための

+0

https://stackoverflow.com/questions/24313539/push-notifications-when-app-is-closedの可能な複製 – ChyperX

+1

Firebase Cloud Messagin(FCM)システムを使用して、アプリが閉じられたときにプッシュ通知を表示できます。実際に私はFirebaseMessagingServiceからこのブロードキャストレシーバーだけを呼びましたが、それでもまだではありませんが、 –

+0

が実装されている[チュートリアル](https://www.simplifiedcoding.net/firebase-cloud-messaging-tutorial-android/)です作業 – Vignesh

答えて

0

[OK]を感謝貴重なコメント私はこのtopiにいくつかのスレッドがあることを知っているcでも何も私のために働かなかったので、これが私を怒らせました。

となりましソリューション:

それは自分の携帯電話でモバイルマネージャーアプリに男だったし、それはいくつかのアプリケーションの自動起動許可を無効にするオプションを持っていると私は自動起動リストに追加され、今では動作しますよく私は他のスマートフォンでこれをXiaomi、Oppo、One Plusのように見てきました。これは私のために働いた。

関連する問題