Fragment中是不是不能用Intent

Fragment中是不是不能用Intent,第1张

Fragment中没有getIntent方法,需要通过Activity来。 可以这样实现: Intent intent = getActivity().getIntent()这样就可以在fragment中获取Activity的Intent

接收推送消息您需要自定义继承自 PushMessageReceiver 类 BroadcastReceiver (必须实现,否则收推送消息)实现其 onNotificationMessageArrivedonNotificationMessageClicked 该 receiver 注册 AndroidManifest.xml 文件

自定义 BroadcastReceiver:

public class DemoNotificationReceiver extends PushMessageReceiver {

@Override

public boolean onNotificationMessageArrived(Context context, PushNotificationMessage message) {

return false

}

@Override

public boolean onNotificationMessageClicked(Context context, PushNotificationMessage message) {

return false

}

}

注册应用 AndroidManifest.xml 面:

<receiver

android:exported="true"

android:name="您自定义 broadcastReceiver 类名">

onNotificationMessageArrived 用接收服务器发通知栏消息(消息达客户端触发)默认return false通知消息融云 SDK 默认形式展现需要自定义通知栏展示实现自通知栏展现代码同 return true 即

onNotificationMessageClicked 用户点击通知栏消息触发 (注意:自定义通知栏展现则触发)默认 return false 需要自定义点击通知跳转return true 即融云 SDK 默认跳转规则

联系发条或者条消息通 intent 隐式启 activityintent uri :

Intent intent = new Intent()

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

Uri.Builder builder = Uri.parse("rong://" + this.getPackageName()).buildUpon()

builder.appendPath("conversation").appendPath(type.getName())

.appendQueryParameter("targetId", targetId)

.appendQueryParameter("title", targetName)

uri = builder.build()

intent.setData(uri)

startActivity(intent)

AndroidManifest.xml 面配置 A activity 拦截 intent 点击条通知栏消息 启 activity A

<activity

android:name="A"

android:launchMode="singleTop"

android:screenOrientation="portrait"

android:windowSoftInputMode="stateHidden|adjustResize">

<data

android:host="包名"

android:pathPrefix="/conversation/"

android:scheme="rong" />

联系发条消息通 intent 隐式启列表 activityintent uri 配置:

Intent intent = new Intent()

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

Uri.Builder builder = Uri.parse("rong://" + this.getPackageName()).buildUpon()

builder.appendPath("conversationlist")

Uri uri = builder.build()

intent.setData(uri)

startActivity(intent)

AndroidManifest.xml 面配置 B activity 拦截 intent点击条通知栏消息 启 activity B

<activity

android:name="B"

android:launchMode="singleTask"

android:screenOrientation="portrait"

android:windowSoftInputMode="stateHidden|adjustResize">

<data

android:host="包名"

android:path="/conversationlist"

android:scheme="rong" />

融云发者台广播推送-广播消息-推送发起远程推送

点击推送消息触发 action 事件:

Intent intent = new Intent()

intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK)

Uri.Builder uriBuilder = Uri.parse("rong://" + this.getPackageName()).buildUpon()

uriBuilder.appendPath("push_message")

.appendQueryParameter("targetId", targetId)

.appendQueryParameter("pushData", pushData)

.appendQueryParameter("pushId", pushId)

.appendQueryParameter("extra", extra)

startActivity(intent)

AndroidManifest.xml 面配置 C activity 拦截 action 点击跳转 activity C

<activity

android:name="C"

android:launchMode="singleTask"

android:screenOrientation="portrait">

<data

android:host="包名"

android:pathPrefix="/push_message"

android:scheme="rong" />

通步骤您已完融云推送服务集

实现要点:主Activity必须设置android:launchMode为singleTask

<activity

android:name="cn.weixq.fscar.MainActivity"

android:launchMode="singleTask" >

</activity>

然后在生成通知栏消息时,要使用Intent传递参数,并且在主Activity中重写OnNewIntent事件来接收参数

// 生成通知栏消息

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE)

Notification notification = new Notification()

notification.icon = R.drawable.tip

// 下面这个是通知刚显示时,在手机屏幕顶部显示的文字

notification.tickerText = "新违章提醒"

notification.when = System.currentTimeMillis()

// 使用所有默认值(声音+震动+灯光)

notification.defaults = Notification.DEFAULT_ALL

// 用户点击通知后删除该通知

notification.flags = Notification.FLAG_AUTO_CANCEL

// 点击通知后要打开的layout(界面)

Intent intent = new Intent(SplashActivity.this, MainActivity.class)

//传递参数

intent.putExtra("flag", "ok")

PendingIntent pendingIntent = PendingIntent.getActivity(SplashActivity.this,0,intent,0)

notification.setLatestEventInfo(SplashActivity.this, "[" + carNo + "]", "有" + jsonArray.length()+ "条新违章,请到[我的车库]中查看!",pendingIntent)

// 创建并显示通知

notificationManager.notify((int) System.currentTimeMillis(),notification)

重写OnNewIntent事件

@Override

protected void onNewIntent(Intent intent)

{

super.onNewIntent(intent)

String flag = intent.getStringExtra("flag")

if (flag != null)

{

// 如果intent不为null,且flag的值为ok,则表示点击了通知栏消息,那么切换到车库界面

radioGroup.check(R.id.radioCar)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11395091.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存