繁体   English   中英

如何向该活动添加待定意图

[英]How to add Pending Intent to this Activity

单击通知后尝试运行活动时遇到问题。 如何以及在何处创建待定意向,在单击通知后必须运行我的应用程序。 我没有通知问题。 但是点击后我的通知不起作用。 请帮忙,谢谢..................................... ................................................... ................................................... ................................................... ...................................................

package pl.wat.pz.myapplication;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends Activity {
private static final String GROUP_UPDATES="group_updates";
private static final String CHANNEL_CONTENT="channel_content";
private static final int NOTIF_ID_CONTENT=1337;
private NotificationManager mgr;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mgr=getSystemService(NotificationManager.class);

    if (mgr.getNotificationChannel(CHANNEL_CONTENT)==null) {

        initContentChannel();
    }

    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.actions, menu);

    return(super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId()==R.id.settings) {
        Intent i=new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
        i.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
        startActivity(i);
    }

    return super.onOptionsItemSelected(item);
}

public void raiseContent(View view) {
    Notification n=new NotificationCompat.Builder(MainActivity.this, CHANNEL_CONTENT)
            .setContentTitle(getString(R.string.notif_content_title))
            .setContentText(getString(R.string.notify_content_text))
            .setSmallIcon(android.R.drawable.stat_sys_warning)
            .build();

    mgr.notify(NOTIF_ID_CONTENT, n);
}

private void initContentChannel() {
    NotificationChannel channel=
            new NotificationChannel(CHANNEL_CONTENT,
                    getString(R.string.channel_name_content),
                    NotificationManager.IMPORTANCE_LOW);

    channel.setGroup(GROUP_UPDATES);
    mgr.createNotificationChannel(channel);
}
}

您对类使用intent来扩展FirebaseMessagingService并且您的intent代码如下所示:

Intent intent = new Intent(context, yourClass.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        final PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        context,
                        requestCode,
                        intent,
                        PendingIntent.FLAG_CANCEL_CURRENT
                );

//然后设置为通知

NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.setBigContentTitle(title);
bigPictureStyle.setSummaryText(Html.fromHtml(message).toString());
bigPictureStyle.bigPicture(bitmap);
Notification notification;
        notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
                .setAutoCancel(true)
                .setContentTitle(title)
                .setContentIntent(resultPendingIntent)
                .setSound(alarmSound)
                .setStyle(bigPictureStyle)
                .setWhen(getTimeMilliSec(timeStamp))
                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
                .setContentText(message)
                .build();

        NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(Config.NOTIFICATION_ID_BIG_IMAGE, notification);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM