【问题标题】:Open Activity without clicking notification in Android在Android中打开Activity而不点击通知
【发布时间】:2015-11-18 13:01:42
【问题描述】:

有没有办法在不点击通知的情况下打开接收推送通知的活动?

示例:- 假设我想在收到推送通知时打开 MainActivity。我可以通过单击通知面板中的通知来做到这一点。但我想在收到通知后立即打开活动(甚至不点击通知)。有可能吗??

【问题讨论】:

    标签: android push-notification


    【解决方案1】:

    在您的 onMessageReceived 方法中,使用以下代码创建 Broadcast

        Intent intent = new Intent();
        intent.setAction("some string");
        //intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        sendBroadcast(intent);
    

    创建一个单独的类并使用 BroadcastReceiver 扩展它。在onReceive里面写如下代码

    Log.d("BroadCastData","BroadCastData");
        Intent newAct = new Intent(context,yourCallClassName.class);
        newAct.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(newAct);
    

    将接收器添加到清单文件的最后一步

    <receiver android:name=".GetingBroadCastData" >  //.GetingBroadCastData is classs name
            <intent-filter>
                <action android:name="some string" />
            </intent-filter>
        </receiver>
    

    希望对您有所帮助。

    【讨论】:

    • 您的 setAction 字符串和清单接收器操作 android 名称字符串应该相同。这是一个工作代码。你能分享代码sn-p.@aditya
    【解决方案2】:

    是的(但这可能取决于您用于接收推送的内容)。

    在您的代码中,当接收到推送时,而不是构建和显示通知,而是创建和广播将打开您的活动的意图。

    【讨论】:

      【解决方案3】:

      我知道现在有点晚了,但它会对 firebase 类中的其他人有用。

       public class FireMsgService extends FirebaseMessagingService {
              public static final String NOTIFICATION_CHANNEL_ID = "10001" ;
              private final static String default_notification_channel_id = "default" ;
              String toke;
              @Override
              public void onMessageReceived(RemoteMessage remoteMessage) {
                  super.onMessageReceived(remoteMessage);
                  String message = remoteMessage.getNotification().getBody();
          }
      

      创建一个广播接收器并添加以下代码。

      public class OnMessageRecieverBroadCast extends BroadcastReceiver {
              public static final String NOTIFICATION_CHANNEL_ID = "10001";
              public static String NOTIFICATION_ID = "notification-id";
              public static String NOTIFICATION = "notification";
              public static String NOTIFICATION_MSG = "notificationmsg";
              String MsgTitle = "";
              String MsgBody;
              public void onReceive(Context context, Intent intent) {
                  if (intent.getExtras() != null) {
                      Log.d("TestTag", "keySet() size" + intent.getExtras().keySet().size());
                      for (String key : intent.getExtras().keySet()) {
                           Object value = intent.getExtras().get(key);
                           MsgBody = (String) intent.getExtras().get("gcm.notification.body");
                           MsgTitle = (String) intent.getExtras().get("gcm.notification.title");
                           Intent intent1 = new Intent(context, NotificationDialogActivity.class);
                           intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                           intent1.putExtra("value", MsgBody);
                           intent1.putExtra("NOTIFICATION_MSG_TITLE", MsgTitle);
                           context.startActivity(intent1);
                  }
              }
          }
      

      onCreate 方法内。

      onCreate(){
               if (getIntent().getExtras() != null) {
                  toke = getIntent().getStringExtra("value");
                  dialogOpenTop(NotificationDialogActivity.this, toke);
                 }
              //add dialog method
              public void dialogOpenTop(final Context context, String toke) {
              final Dialog dialog = new Dialog(NotificationDialogActivity.this);
              dialog.setContentView(R.layout.alert);
              dialog.getWindow().setBackgroundDrawable(new 
              ColorDrawable(Color.TRANSPARENT));
              dialog.setCancelable(false);
              dialog.show    
             }
      

      在清单中:

       <service
              android:name=".notification.FireMsgService"
              android:exported="false">
              <intent-filter>
                  <action android:name="com.google.firebase.MESSAGING_EVENT" />
              </intent-filter>
          </service>
          <receiver
              android:name=".notification.OnMessageRecieverBroadCast"
              android:exported="true"
              android:permission="com.google.android.c2dm.permission.SEND">
              <intent-filter>
                  <action android:name="com.google.android.c2dm.intent.RECEIVE" />
              </intent-filter>
          </receiver
       >
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-01
        • 2017-04-04
        • 1970-01-01
        相关资源
        最近更新 更多