0

アンドロイドでfirebaseプッシュ通知に取り組んでいますが、これを正常に実装しました。私のアプリがバックグラウンドで実行されているときだけ、アプリが閉じているとき(バックグラウンドではない)、プッシュをタップすると、最初のアクティビティ(主なアクティビティ)からアプリが起動します。だから、どんな仲間も私がそれを理解するのを助けることができる、私はここに私のコードを掲載しています。アプリが閉じている間にプッシュ通知のタップでアクティビティを開始する方法(バックグラウンドでさえも)

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMsgService"; 
    public static String PUSH_NOTIFICATION = "PUSH_NOTIFICATION"; 
    String store_id, img_url; 
    String store_name; 
    String headline; 
    String store_img; 
    int size = 200; 
    String subtext; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     size = (int) Math.ceil(Math.sqrt(300 * 200)); 
     Message message = new Message(); 
     message.obj = remoteMessage; 
     handler.sendMessage(message); 
     Intent pushNotification = new Intent(this.PUSH_NOTIFICATION); 
     LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); 
    } 


    private Handler handler = new Handler() { 
     @Override 
     public void handleMessage(Message msg) { 
// TODO(developer): Handle FCM messages here. 
      Log.d(TAG, "=====push msg------by jigar--" + msg.toString()); 
      final RemoteMessage remoteMessage = (RemoteMessage) msg.obj; 
      if (remoteMessage.getData().size() > 0) { 
       Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
       final JSONObject jsonObject; 
       try { 
        jsonObject = new JSONObject(remoteMessage.getData().toString()); 
        Log.d(TAG, "tag: " + jsonObject.getString("store_id")); 
        Log.d(TAG, "message: " + jsonObject.getString("img_url")); 
        store_id = jsonObject.getString("store_id"); 
        img_url = jsonObject.getString("img_url"); 
        headline = jsonObject.getString("headline"); 
        store_name = jsonObject.getString("store_name"); 
        store_img = jsonObject.getString("store_image"); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 

        Glide.with(getApplicationContext()) 
          .load(img_url) 
          .asBitmap() 
          .into(new SimpleTarget<Bitmap>() { 
           @Override 
           public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) { 
            ByteArrayOutputStream out = new ByteArrayOutputStream(); 
            resource.compress(Bitmap.CompressFormat.PNG, 100, out); 
            Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray())); 

            sendNotification(remoteMessage.getNotification().getBody(), decoded, store_id, remoteMessage.getNotification().getTitle()); 
           } 
          }); 
       } else { 
        sendNotification("", null, store_id, ""); 
       } 
      } 
     } 
    }; 

    private void sendNotification(String messageBody, Bitmap bitmap, String storeId, String subtext) { 
     Intent intent = null; 
     intent = new Intent(this, ProductDescriptionActivity.class); 
     intent.putExtra("storeid",store_id); 
     Const.STORE_ID = store_id; 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT|PendingIntent.FLAG_UPDATE_CURRENT); 
     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
      Log.e("===BITMAP ", bitmap + ""); 
      NotificationCompat.BigPictureStyle notyStyle = new NotificationCompat.BigPictureStyle(); 
      notyStyle.bigPicture(bitmap); 
      mBuilder.setSmallIcon(R.drawable.ic_launcher) 
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), 
          R.drawable.ic_launcher)) 
        .setContentTitle(headline).setColor(getResources().getColor(R.color.orange)) 
        .setContentText(store_name + "\n" + messageBody).setContentInfo(subtext) 
        .setAutoCancel(true) 
        .setSound(defaultSoundUri) 
        .setContentIntent(pendingIntent) 
        .setStyle(notyStyle); 
     } else { 
      mBuilder.setSmallIcon(R.drawable.ic_launcher) 
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), 
          R.drawable.ic_launcher)) 
        .setContentTitle(headline) 
        .setContentText(messageBody) 
        .setAutoCancel(true) 
        .setSound(defaultSoundUri) 
        .setContentIntent(pendingIntent); 
     } 
     NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     Random r = new Random(); 
     notificationManager.notify(r.nextInt(80 - 65) + 65, mBuilder.build()); 
     PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE); 
     PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG"); 
     wl.acquire(6000); 
    } 
} 
+0

マニフェスト –

答えて

0

AndroidManifest.xmlファイルのMainActivityタグにbelow属性を追加してください。

android:launchMode="singleTask" 
+0

mainActivityまたは起動したいアクティビティを投稿してください。 –

+0

はい。最初の起動アクティビティです。 – Elango

+0

これは動作しません。何も変更されていません。 –

0

私のMainActivityを開くことができます。

NotificationManager notificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(icon, message, when); 

    Intent notificationIntent = new Intent(context, MainActivity.class); 

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
      | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

    PendingIntent intent = PendingIntent.getActivity(context, 0, 
      notificationIntent, 0); 

    notification.setLatestEventInfo(context, title, message, intent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notificationManager.notify(0, notification); 
+0

動作しません。 –

+0

私の質問を最初にチェックしてください。 –

関連する問題