相关疑难解决方法(0)

如何从通知点击发送参数到活动?

我可以找到一种方法从我的通知中向我的活动发送参数.

我有一个创建通知的服务.当用户点击通知时,我想用一些特殊参数打开我的主要活动.例如项目ID,因此我的活动可以加载并呈现特殊项目详细信息视图.更具体地说,我正在下载一个文件,当下载文件时,我希望通知有一个意图,当点击它时,它会以特殊模式打开我的活动.我试图使用putExtra我的意图,但似乎无法提取它,所以我认为我做错了.

我的服务中创建通知的代码:

        // construct the Notification object.
     final Notification notif = new Notification(R.drawable.icon, tickerText, System.currentTimeMillis());


    final RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);
    contentView.setImageViewResource(R.id.image, R.drawable.icon);
    contentView.setTextViewText(R.id.text, tickerText);
    contentView.setProgressBar(R.id.progress,100,0, false);
    notif.contentView = contentView;        

    Intent notificationIntent = new Intent(context, Main.class);
    notificationIntent.putExtra("item_id", "1001"); // <-- HERE I PUT THE EXTRA VALUE
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notif.contentIntent = contentIntent;

    nm.notify(id, notif);
Run Code Online (Sandbox Code Playgroud)

我的Activity中的代码尝试从通知中获取额外参数:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);


    Bundle extras = getIntent().getExtras();
    if(extras != null){
        Log.i( "dd","Extra:" + …
Run Code Online (Sandbox Code Playgroud)

notifications android bundle android-intent

193
推荐指数
7
解决办法
14万
查看次数

不调用onNewIntent

我的情况很奇怪.
有一个应用程序,我决定从第一个应用程序的代码创建另一个.
我复制了.xml文件,复制了.java文件以便一切正常.
但是有一个巨大的问题:我的onNewIntent(Intent intent)方法是在第一个项目中调用的,但它没有在第二个项目中调用(代码是相同的!)

方法,可以触发,但现在无法触发

public void onClick(View arg0) {
    Intent browserInt = new Intent (Intent.ACTION_VIEW, 
    Uri.parse("https://oauth.yandex.ru/authorize?response_type=token&client_id=zzzzz"));
    browserInt.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(browserInt);
}
Run Code Online (Sandbox Code Playgroud)

这是onNewIntent()方法:

@Override
protected void onNewIntent(Intent intent){
    System.out.println(" I WORKED!");
    Uri uri = intent.getData();
    if (uri!=null) {
        String m = uri.toString().split("#")[1];
        String[] args = m.split("&");
        String arg = args[0];
        String token = arg.split("=")[1];
        System.out.println(token);
    }   
}
Run Code Online (Sandbox Code Playgroud)

遗憾的是,我在日志中看不到"我工作".
我在SO和Internet上都阅读了很多类似的问题,尝试设置Intent标志SINGLE_TOP,SINGLE_TASK等等.

这是Android Manifest的WORKING项目:

<application 
    android:name="yyy"
    android:icon="@drawable/yaru_icon"
    android:allowBackup="false"
    android:label="xxx"
    android:theme="@style/LightTheme">

    <activity
        android:name=".Main"
        android:label="xxx"
        android:launchMode="singleTask">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application> …
Run Code Online (Sandbox Code Playgroud)

android android-intent

29
推荐指数
3
解决办法
3万
查看次数

点击通知进入当前活动

我正在使用这个解决方案:如何使通知意图恢复而不是制定新的意图?

当我正常运行我的应用程序时工作正常..但我的应用程序有共享功能.

当我选择要从图库应用程序共享的图像并在我的活动中打开它时,我在活动中创建通知.我希望当用户点击通知时,它会打开现有活动(由图库应用打开)

问题是,当我点击通知时,它不会恢复该活动,而是打开活动的新实例或之前已打开的另一个实例

编辑:更多解释

我的应用程序名为ShareApp,活动名为ShareActivity,问题是当我通过图库应用程序打开ShareActivity时,会在图库应用程序任务的堆栈顶部创建一个ShareActivity实例.现在,当我创建指向我的ShareActivity的通知时,我使用了intent:

Intent intent = new Intent(this, ShareActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Run Code Online (Sandbox Code Playgroud)

问题是,当我点击通知时,它指向ShareApp任务的堆栈中的ShareActivity实例,而不是gallery app任务的堆栈.

任何想法如何指向正确的任务的堆栈?

编辑2:我的代码

        int defaults = Notification.FLAG_NO_CLEAR;
        NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle(getString(R.string.app_name))
                        .setContentText(text)
                        .setDefaults(defaults);
        Intent intent = new Intent(this, this.getClass());
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        mBuilder.setContentIntent(pendingIntent);
        NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(_ID, mBuilder.build());
Run Code Online (Sandbox Code Playgroud)

编辑3:adb shell dumpsys活动(在应用David代码之后/sf/answers/1183112241/)

我的应用程序名为shareApp,我的活动是ShareActivity

在点击通知之前:

Main stack:
    TaskRecord{41965708 #6 A com.android.gallery3d}
    Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.gallery3d/.app.Gallery bnds=[364,50][108,108]}
      Hist #2: ActivityRecord{417ccc28 com.yeahman.shareapp/.ShareActivity}
        Intent …
Run Code Online (Sandbox Code Playgroud)

notifications android

11
推荐指数
3
解决办法
3万
查看次数

关闭的应用程序未调用 Xamarin Android OnNewIntent

什么是工作

我已经使用以下 Xamarin 指南为 Xamarin Forms 应用程序的 Android 端实现了推送通知:

https://developer.xamarin.com/guides/android/application_fundamentals/notifications/remote-notifications-with-gcm/

这很有效,我们正确地在应用程序运行时和在后台调用 OnNewIntent。然后我们调用我们的方法来处理推送通知的意图,一切都很好。

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);
    GoogleListenerService.ReceivedRemoteNotification(intent);
}
Run Code Online (Sandbox Code Playgroud)

问题是什么

如果我们关闭应用程序,触发推送通知,然后单击推送通知,应用程序似乎不会调用 OnNewIntent,而是会像单击应用程序图标(调用 OnCreate)一样启动应用程序。

尝试了什么

我尝试实施的主要解决方法概述如下:

  1. 未调用 Android OnNewIntent

Xamarin 只有一个 GetIntent 方法,需要字符串 URL 输入,并使用以下结果在空数据中。

Intent intent = new Intent(this, typeof(MainActivity));
Bundle data = intent.Extras;
Run Code Online (Sandbox Code Playgroud)
  1. 未调用 onNewIntent

在第二篇文章中,我们的意图服务、MainActivity.cs 文件和清单文件中 LaunchMode = SingleTop 的所有更改都没有导致行为发生任何变化。

编辑

推送通知总是被接收并且 OnMessageReceived 总是被调用,无论应用程序是在后台还是完全关闭。此外, OnNewIntent 在应用程序处于后台时接收挂起的意图,而不是在它关闭时。

this.Intent.Extras;
Run Code Online (Sandbox Code Playgroud)

始终显示为 null(当前位于 OnResume 方法中)。

android-intent xamarin.android xamarin

7
推荐指数
1
解决办法
2280
查看次数