在Android

时间:2015-11-18 13:01:43

标签: android push-notification

有没有办法在接收推送通知时打开活动而不点击通知?

示例: - 假设我想在收到推送通知时打开MainActivity。我可以通过单击通知面板中的通知来完成此操作。但是我想在收到通知后立即打开活动(甚至没有点击通知)。有可能??

3 个答案:

答案 0 :(得分:0)

是(但这可能取决于你用来接受推送的内容)。

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

答案 1 :(得分:0)

onMessageReceived 方法中,使用以下代码创建广播

    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>

愿这对你有所帮助。

答案 2 :(得分:0)

我知道现在有点晚了,但它对 firebase 类中的 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
 >